我正在运行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的替代方法
答案 0 :(得分:0)
您可以使用json_decode()函数将JSON字符串转换为对象,添加变量,然后将对象转换回JSON
$obj = json_decode($json, true);
$obj->feed[0]->url = "google.com"
:
答案 1 :(得分:0)
代码:
<?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;
答案 2 :(得分:0)
解码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());
?>
答案 3 :(得分:0)
这是一些与我正在寻找的东西有多么相关,这个答案发现@ Is there a JSON api based CMS that is hosted locally?
最佳答案我的问题
或者如果您感到困惑,可以查看