2013-08-19 99 views
2

我有具有每JSON对象的直接图像链接和文件夹名称简单的JSON文件:追加阵列数据作为对象来JSON文件

[ 
     { 
       "image": "http://placehold.it/350x150", 
       "folder": "Small" 
     }, 
     { 
       "image": "http://placehold.it/450x250", 
       "folder": "Medium" 
     }, 
     { 
       "image": "http://placehold.it/550x350", 
       "folder": "Medium" 
     } 
] 

我想这两个值从后追加,但结果是一个不变的json文件。下面是PHP代码瓦特/评论:

$directLink = $_POST['txtDirectLink']; 
$category = $_POST['selectCategoriesImages']; 
$util->addToJsonImageList($directLink, $category); 

//decodes the json image list, merges to the decoded array a new image, then encodes back to a json file 
function addToJsonImageList($link, $category) { 
    $file = file_get_contents('./images_list.json', true); 
    $data = json_decode($file); 
    unset($file); 

    $newImage = array('image' => $link, 'folder' => $category); 
    //$newImage['image'] = $link; 
    //$newImage['folder'] = $category; 
    $result = array_merge((array)$data, (array)$newImage); 

    json_encode($result); 
    file_put_contents('./images_list.json', $result); 
    unset($result); 
} 

的逻辑是,它应该是简单的json_decode该文件作为一个阵列,所述新的数组,并到的是,然后将结果进行编码并投入相同的文件。我一直无法捕捉任何类型的错误。

+0

尝试'$ data = json_decode($ file,true);' – bansi

+0

我以为我已经这样做了,以确保它作为一个数组返回,但我偶然在文件中添加了bool参数。但仍然没有改变。 – benbob

+0

file_put_contents('./images_list。json',json_encode($ result)); –

回答

5
$data = json_decode($file, true); 
//When TRUE, returned objects will be converted into associative arrays. 
unset($file); 

$newImage = array('image' => $link, 'folder' => $category); 
//$newImage['image'] = $link; 
//$newImage['folder'] = $category; 
$result = array_merge($data, $newImage); 
//don't need to cast 

参考PHP json_decode手册

编辑下面的代码工作(测试)

function addToJsonImageList($link, $category) { 
    $file = file_get_contents('./images_list.json', true); 
    $data = json_decode($file,true); 
    unset($file); 
    //you need to add new data as next index of data. 
    $data[] = array('image' => $link, 'folder' => $category); 
    $result=json_encode($data); 
    file_put_contents('./images_list.json', $result); 
    unset($result); 
} 

编辑2增加了很多错误报告和调试。请让我知道以下的输出。下面的代码没有经过测试(只是在这里输入)。如果发现任何语法错误,请修复。这里迟到,只能明天我的时间,但可以回复。

<?php 
//display all errors and warnings 
error_reporting(-1); 
addToJsonImageList('test link', 'test category'); 

function addToJsonImageList($link, $category) { 
    $file = file_get_contents('./images_list.json', true); 
    if ($file===false)die('unable to read file'); 
    $data = json_decode($file,true); 
    if ($data ===NULL)die('Unable to decode'); 
    unset($file); 
    echo "data before\n"; 
    var_dump ($data); 
    //you need to add new data as next index of data. 
    $data[] = array('image' => $link, 'folder' => $category); 
    echo "data after\n"; 
    var_dump ($data); 
    $result=json_encode($data); 
    if (file_put_contents('./images_list.json', $result) === false){ 
     die('unable to write file'); 
    } 
    unset($result); 
} 
?> 
+0

我现在收到“array_merge:argument#1不是数组”错误。 – benbob

+0

我试过编辑后的代码,它仍然没有更改我的json文件。是否有错误检查我可以用来查看问题的位置? json文件与此代码位于同一目录中。 – benbob

+0

真棒,与您的错误检查我能够得到它的工作。有一件事我注意到了json文件的不同之处: 任何http://字符串都变成了http:\/\ /(编码是这样做的吗?)。感谢您的帮助和时间,我会看看逃生斜线 – benbob

0

你可以做的是解码json然后合并它们。

$data = json_decode(file_get_contents(./images_list.json)); 

$result = array_merge((array)$data, (array)$newImage); 

,然后就输出json_encode

file_put_contents('./images_list.json', json_encode($result, JSON_FORCE_OBJECT)); 
+0

我在file_put_content里面添加了json_encode(result),现在我收到错误:“array_merge():参数#1不是数组”,即使我将$ data设置为数组而不是对象,通过添加可选的true – benbob

0

你的问题是与代码

以下行
json_encode($result); 
file_put_contents('./images_list.json', $result); 

你没有捕捉json_encode让你写一个array到文件的结果。

你需要调整的代码如下所示

$result = json_encode($result); 
file_put_contents('./images_list.json', $result); 
+0

我应该更新我的答案,因为我已经解决了这个问题。感谢您的洞察力。 – benbob

1

比方说你有一个名为(playerJson)

{ 

"Players": 
    [ 
    {"Name":"Arun","Arm":"Gun","num":"1"}, 
    {"Name":"sssv","Arm":"Arc","num":"2"}, 
    {"Name":"Surya","Arm":"Bomb","num":"3"}, 
    {"Name":"sssv","Arm":"Fire","num":"4"} 

    ] 
} 

现在我们必须在PHP这样做以.json文件(myPhpFile .php)是:(我想你是从表单加载你的数据)

<?php 

$json = file_get_contents('playerJson.json'); 
$json_data = json_decode($json,true); 

$newar = array(
      'Name'=>$_POST['nameField'] , 
      'Arm' =>$_POST['armField'], 
      'Num' =>$_POST['numField'] 
); 
//saving data in Players object... 
array_push($json_data['Players'], $newar); 

$json = json_encode($json_data); 

file_put_contents('playerJson.json', $json); 

?> 

就这样!你在“玩家”对象中有一个新行。 但是,如果您想添加一个新对象,请避免array_push函数中的$ json_data之后的[Players]。