2015-02-07 187 views
0

我有一個JSON文件存儲在我需要添加數據的服務器上。我已成功將文件檢索到內存中,並已成功解析它。我只是很難將新的JSON數據放在正確的位置。將JSON數據附加到與PHP位於服務器上的JSON文件

這裏是我的JSON文件的格式:

{ 
    "emails": [ 

       { 
       "group": "1st Group", 
       "list": [ 
          { 
          "id": 1, 
          "subject": "Testing 1" 
          }, 
          { 
          "id": 2, 
          "subject": "Testing 2" 
          } 
         ] // End list array 
       }, // End 1st Group 

       { 
       "group": "2nd Group", 
       "list": [ 
          { 
          "id": 3, 
          "subject": "Testing 3" 
          }, 
          { 
          "id": 4, 
          "subject": "Testing 4" 
          } 
         ] // End list array 
       } // End 2nd Group 

       /* NEED TO INSERT NEW DATA HERE */ 

      ] // End emails array 
} 

我想最後grouplist後附加新grouplist。在這個例子中,這將在這行後面:} // End 2nd Group

這裏是我的PHP代碼從我的服務器獲取JSON文件,並對其進行解析:

$getJSON = file_get_contents('emails.json'); 
$tempArray = json_decode($getJSON, true); 

$numberOfGroups = count($tempArray['emails'][0]); 

這裏是我的PHP代碼,創建我的JSON文件的格式/佈局:

$groupArray = array(); 

$json = array('emails' => array()); 

foreach ($contextIORequest->getData() as $message) { 

    $newTZ = new DateTimeZone("America/Chicago"); 
    $currentTime = new DateTime(); 
    $currentTime = DateTime::createFromFormat('U', $message['date_received']); 
    $currentTime->setTimezone($newTZ); 
    $formattedDateReceived = $currentTime->format('F j, Y'); 

    if (!in_array($formattedDateReceived, $groupArray)) { 
     array_push($json['emails'], 
      array(
       'group' => $formattedDateReceived, 
       'list' => array() 
     ) 
    ); 
     $groupArray[] = $formattedDateReceived; 
    } 

    $body = str_replace(array("\r\n","\n"),"", $message['body'][0]['content']); 
    $newBody = preg_replace('!\s+!', ' ', $body); 

    array_push($json['emails'][array_search($formattedDateReceived,$groupArray)]['list'], 
     array(
     'id' => $message['message_id'], 
     'subject'=> addslashes($message['subject']), 
     'to' => array("Me"), 
     'body' => $newBody, 
     'time' => $formattedDateReceived, 
     'datetime' => $formattedDateReceived, 
     'from' => $message['addresses']['from']['name'], 
     'dp' => "assets/img/profiles/avatar.jpg", 
     'dpRetina' => "assets/img/profiles/avatar2x.jpg" 
    ) 
); 

} // end foreach loop 

// Output the JSON 
header('Content-Type: application/json'); 
echo json_encode($json); 

那麼我怎麼會去追加一個新的grouplist後最後grouplist?這需要完成,但實際上並未包含用於創建JSON的emails數組。

任何幫助表示讚賞!

回答

0

其上運行的串行數據json_decode(如與像true你所做通過第二PARAM)後,你就會有,在PHP中,其中有一處名爲emails關鍵一個元素的關聯數組。該元素是一個數組,並擁有您想添加另一個的組列表。因此,它的問題:

$data = json_decode($getJSON, true); 
$data['emails'][] = $TheNewGroupList; 

別讓它開始作爲JSON格式序列化的數據讓你疲憊的事實。一旦你運行了json_decode它只是一個PHP數據結構(對象或數組),並且對它的操作就像任何其他PHP數據一樣。

+0

感謝您的回覆。我需要追加的新JSON數據嵌套在'emails' php數組中。我只需要追加位於'emails' php數組內的內容。你明白我的意思嗎? – three3 2015-02-07 03:44:55

+0

你已經提供了你的開始JSON的樣本。請提供需要附加的數據樣本,也許我會更好地理解你。 – JAAulde 2015-02-07 03:47:14

+0

本週早些時候,我在StackOverflow上獲得了幫助。這個答案位於這裏有我的PHP代碼:http://stackoverflow.com/a/28310533/317740 正如你在代碼中看到的,所有的數據都嵌套在一個'emails'數組中。我需要將'emails'數組中的所有數據都追加,但不包括實際的'emails'數組。這現在有道理嗎? – three3 2015-02-07 03:51:12