【问题标题】:How to update or add value to JSON [duplicate]如何更新或向 JSON 添加值 [重复]
【发布时间】:2017-01-06 02:48:49
【问题描述】:

我正在运行一个来自 AndroidHive 的应用程序,该应用程序具有 feed.json,该应用程序从中加载以下内容。

http://api.androidhive.info/feed/feed.json 这是帖子网址http://www.androidhive.info/2014/06/android-facebook-like-custom-listview-feed-using-volley/

{
    "feed": [{
                "id": 1,
                "name": "National Geographic Channel",
                "image": "A URL",
                "status": "Science and etc"
                And Cosmos is all about making science an experience.
                ","
                profilePic ": "
                A URL ","
                timeStamp ": "
                1403375851930 ","
                url ": null
    }]
}

以上是默认内容,我想知道如何从或使用 php 脚本向该内容添加更多内容。我很想知道怎么做,因为我注意到在 www_androidhive_info 中创建的大多数应用程序源代码都使用 .json 来加载内容,这是 webview 的替代方法

【问题讨论】:

    标签: php android json


    【解决方案1】:

    您可以使用json_decode() 函数将 JSON 字符串转换为对象,添加变量,然后将对象转换回 JSON

    $obj = json_decode($json, true);
    $obj->feed[0]->url = "google.com"
    :
    

    【讨论】:

      【解决方案2】:
      1. 加载 JSON
      2. 解码 JSON
      3. 添加您的内容
      4. 编码为 JSON
      5. 将其发送到您的应用程序

      代码:

      <?php
      
      function failure($reason) {
          header('HTTP/1.1 500 Internal Server Error');
          echo json_encode(['failure' => $reason]);
          exit;
      }
      
      // 1. Load the JSON
      $content = file_get_contents('http://api.androidhive.info/feed/feed.json');
      if (!$content) {
          return failure('Failed to load upstream JSON');
      }
      
      //  2. Decode the JSON
      $decodedContent = json_decode($content, true);
      if ($decodedContent === false) {
          return failure('Failed to parse upstream JSON');
      } elseif (empty($decodedContent)) {
          return failure('Upstream JSON empty');
      }
      
      // 3. Add your content
      $decodedContent['feed'][] = [
          "id": 12,
          "name": "An example name",
          "image": "https://example.com/test_image.jpg",
          "status": "Hello!",
          "profilePic": "https://example.com/test_profile_pic.jpg",
          "timeStamp" => time(),
          "url" => "https://example.com"
      ];
      
      // 4. Encode to JSON
      $responseContent = json_encode($decodedContent);
      
      // 5. Send it to your app
      echo $responseContent;
      

      【讨论】:

      • 这可能是一个愚蠢的问题,但我问的原因我很困惑。如果是的话,我是否要将上面的代码保存到 .php 文件中?已经将其复制并保存为 test.php,但在使用 wamps 的意义上,我正在离线运行它。当我测试运行 test.php Parse error: parse error, expecting `')'' in C:\wamp\www\feed\test.php 的第 5 行时,我遇到了这样的错误,第 5 行是 echo json_encode(['失败' => $原因]);我真的希望我的添加和编辑就像在输入数字一样,当我单击提交时就像在注册或发布它更新 .json 文件(自动)
      • 再次检查您的 php 版本。只有 5.6+ 正在接收安全更新。至于您的其他问题,您需要一个完整的批次代码才能拥有一个管理系统并与第三方保持最新的提要。
      • 我的 wamp php 版本是 5.3.0 我怎样才能获得管理系统脚本或制作一个? stackoverflow.com/questions/22582471/…
      【解决方案3】:

      解码 JSON 并为更改创建新函数,然后重新编码。

      <?php
      $JsonVar = file_get_contents('http://api.androidhive.info/feed/feed.json');
      $myjson = json_decode($JsonVar,true);
      
      function Changes() {
          $myjson->feed[2]->name = "FeedID2";
          $myjson->feed[2]->id = "2";
          $myjson->feed[2]->image = "link to image.png";
          $myjson->feed[2]->status = "I am here.";
          $myjson->feed[2]->profilePic = "link to image.png";
          $myjson->feed[2]->timeStamp = time();
          $myjson->feed[2]->url = 'http://url.com';
      }
      
      json_encode(Changes());
      ?>
      

      【讨论】:

      • 我怎样才能将它编译成一个有效的自动 .php 脚本,就像我在点击提交时发布新内容一样,如果可能的话,它将更新 .json
      • @SIR-MYK3 - 检查this answer.
      猜你喜欢
      • 1970-01-01
      • 2017-09-16
      • 2017-02-10
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-01
      相关资源
      最近更新 更多