2015-02-07 84 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