2016-08-30 43 views
0

我正在運行AndroidHive的應用程序,該應用程序有feed.json應用程序從其下載內容。如何更新或增加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

+3

[在php中解析JSON數組並將值添加到php數組中](http://stackoverflow.com/ques tions/17707708/parse-json-array-in-php-and-add-values-to-php-array) –

回答

0

可以使用json_decode()功能JSON字符串轉換爲對象,添加變量,然後CONVER對象回JSON

$obj = json_decode($json, true); 
$obj->feed[0]->url = "google.com" 
: 
0
  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; 
+0

這可能是一個愚蠢的問題,但我問的原因很混亂。我是否會將上面的代碼保存到.php文件中,如果是的話,它已經將其複製並保存爲test.php,但我正在使用wamps離線運行它。我得到這樣的錯誤,當我測試運行test.php解析錯誤:解析錯誤,期待'')''在第5行的C:\ wamp \ www \ feed \ test.php哪5行是echo json_encode([''失敗'=> $ reason]);我真的希望我的添加和編輯就像輸入數字一樣,當我點擊提交它時更新.json文件(自動) –

+0

仔細檢查您的PHP版本。只有5.6+正在接收安全更新。至於你的額外問題,你需要一個很大的代碼來建立一個管理系統,並保持飼料與第三方的最新。 – jedifans

+0

我的wamp php版本是5.3.0我如何獲得管理系統腳本或製作一個? http://stackoverflow.com/questions/22582471/is-there-a-json-api-based-cms-that-is-hosted-locally –

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()); 
?> 
+0

我如何編譯它是一個有效的自動.php腳本將是就像我發佈新內容時提交它將更新.json,如果它posible –

+0

@ SIR-MYK3 - 檢查[此答案。](http://stackoverflow.com/a/39213067/6486232) – protld