使用php-json API进行模拟,类似于xml的json结构

时间:2014-12-17 22:57:56

标签: php xml json

大家好日子。我试图模拟(使用php),json的类似xml的结构 如下:



{
    "Articles": [
        {
            "Article": {
                "ID": 111,
                "title": Origin of man",
                "author": "Man and Woman",
                "pubDate": "21st Feb, 2014", 
                "summary": "Both Idiots"
            }
        },
        {
            "Article": {
                "ID": 222,
                "title": "Origin of Animals",
                "author": "Animal and Man",
                "pubDate": "22nd Feb, 2014", 
                "summary": "Yet another Idiots"
            }
        },
        {
            "Article": {
                "ID": 333,
                "title": "Origin of Politicians",
                "author": "Politicians and Man",
                "pubDate": "23rd Feb, 2014", 
                "summary": "Inexplicable Idiots"
            }
        }
    ]
}



 在这里,"文章"是json的根节点等价(在xml中) 和"文章"是子节点,具有不同的ID。 我的问题是,我如何以编程方式添加子子 (或分记录)由222标识的文章,如此 entires结构变成这样:



{
    "Articles": [
        {
            "Article": {
                "ID": 111,
                "title": Origin of man",
                "author": "Man and Woman",
                "pubDate": "21st Feb, 2014", 
                "summary": "Both Idiot"
            }
        },
        {
            "Article": {
                "ID": 222,
                "title": "Origin of Animals":
                "SUB-TITLES": 
                           { 
                             "SUB_TITLE_1": "Just kidding",
                             "SUB_TITLE_2": "Still Kiddng",
                             "SUB_TITLE_3": "Dont be offended"
                           }     
                "author": "Animal and Man",
                "pubDate": "22nd Feb, 2014", 
                "summary": "Yet another Idiots"
            }
        },
        {
            "Article": {
                "ID": 333,
                "title": "Origin of Politicians",
                "author": "Politicians and Man",
                "pubDate": "23rd Feb, 2014", 
                "summary": "Inexplicable Idiots"
            }
        }
    ]
}




这是我的尝试,但看起来不够好 虽然一切顺利但没有错误, 但输出并不是我实际期待的。在这里:



{ 
   "Articles": [
       {
           "Article":  {
               "ID":111,
               "title":"Origin of man",
               "author":"Man and Woman",
               "pubDate":"21st Feb, 2014",
               "summary":"Both Idiot"
           }
       },

       {
           "Article":  {
               "ID":222,
               "title":"Origin of Animals":
                       //i expected to have SUB-TITLES record here, but didnt
               "author":"Osagie Odigie",
               "pubDate":"22nd Feb, 2014",
               "summary":"Yet another Idiots"
            }
       },

       {
            "Article": {
                "ID":333,
                "title": "Origin of Politicians",
                "author": "Politicians and Man",
                "pubDate": "23rd Feb, 2014", 
                "summary": "Inexplicable Idiots"
            }
       }
    ], 
    //Now, here's where SUB-TITLES record showed up
    "0":{ "SUB-TITLES": [
             {"SUB_TITLE_1": "Just kidding"},
             {"SUB_TITLE_2": "Still Kiddng"},
             {"SUB_TITLE_3": "Dont be offended"}
           ]
         }
}
 



 事实上,我无法告诉我到底做错了什么,我 也不能告诉消息来源零 在" SUB-TITLES"之前的开口大括号:我也不能说 来自大括号即将到来。任何指导或代码snipette 关于如何做得好将受到高度赞赏。谢谢 期待我的帮助。



Sure ! i anticipate writing it to my file for permanent
storage if all goes fine. for now, i'm just displaying it on my browser
using: 

 echo json_encode($data);

Here's an extract of the code:

 function JSONAddRecordToChild(array $Record, $ChildID){
        
        
        $file = file_get_contents("File.json", true);
        $data = json_decode($file, true);                   //get json by assoc array
        unset($file);                                    //prevent memory leaks for large json 
        
        //only when ID does not exist, write record to json 
        $node = $this->searchJSON($data["Articles"], $ChildID);         //costumized searchJSON methode that returns a matching node
        if($node != null){
            
            $node = array("SUB-TITLES" => array()); 
            
            foreach ($Record as $key => $value){ 
                $node["SUB-TITLES"][] = array($key => $value);
            }
            
            foreach ($data as $child){
                foreach ($child as $son){ 
                    if($son["Article"]['ID'] == $ChildID){ 
                        $data[] = $node;
                    }  
                }
            } 

           echo json_encode($data).;  

           //to be written permanently to file as mentioned
        } 
    }




1 个答案:

答案 0 :(得分:0)

看起来问题出在你上一个foreach循环中。您正在检查内部元素$son是否与所需的ID匹配,但是您要在根级$data上设置子标题

TL; DR Change

if($son["Article"]['ID'] == $ChildID){ 
    $data[] = $node;
}  

if($son["Article"]['ID'] == $ChildID){ 
    $son["Article"][] = $node;
}