0

我有以下 json 文件。我需要 php 中的代码来解析它。我尝试了所有可能的方法,但没有运气。我不是 json 解析方面的专家。

{
     "_id" : { "oid" : "5213785fe4b0780ba56884d3" },
     "author" : "Remate.ph",
     "message" : "Suspected ASG abducts trader in Zamboanga City http://t.co/4hUttNPI",
     "time_created" : 1357958119000,
     "version" : "v2.1",          
},
{
     "_id" : { "oid" : "5213785fe4b0780ba56884d6" },
     "author" : "Clydepatrick Jayme ",
     "message" : "RT @iFootballPlanet: 74' Osasuna 0 - 0 Real Madrid\n\n-ASG.",
     "time_created" : 1358022721000,
     "version" : "v2.1",               
}

我通过以下方式修改了上面的json文件。我为它编写了 php 解析代码,它工作正常。

{
"info1":{
                "_id" : { "oid" : "5213785fe4b0780ba56884d3" },
                "author" : "Remate.ph",
                "message" : "Suspected ASG abducts trader in Zamboanga City http://t.co/4hUttNPI",
                "time_created" : 1357958119000,
                "version" : "v2.1",

},
"info2":{
                "_id" : { "oid" : "5213785fe4b0780ba56884d6" },
                "author" : "Clydepatrick Jayme ",
                "message" : "RT @iFootballPlanet: 74' Osasuna 0 - 0 Real Madrid\n\n-ASG.",
                "time_created" : 1358022721000,
                "version" : "v2.1",

}
}

我使用下面的代码来解析它。

<?php
//$string=file_get_contents("/Users/Anirudh/Downloads/test.json");
$string=file_get_contents("/Users/Anirudh/Sem-1/RA/Test/test.json");
$json_a=json_decode($string,true);

$jsonIterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($json_a),RecursiveIteratorIterator::SELF_FIRST);
//$jsonIterator = new RecursiveArrayIterator(json_decode($string, TRUE));
foreach ($jsonIterator as $key => $val) {
echo "$key => $val\n";
}
?>

有人可以帮助我获得使用 PHP 解析的第一个 json 格式吗?

4

1 回答 1

1

您的 json 文件有两个错误:

  1. [用逗号分隔的两个 1 级项目(info1 和 info2)必须是一个数组,用括号和包裹整个字符串]
  2. 最后 2 级项目(版本) - 删除尾随逗号"version" : "v2.1",

更正的json:

[{
     "_id" : { "oid" : "5213785fe4b0780ba56884d3" },
     "author" : "Remate.ph",
     "message" : "Suspected ASG abducts trader in Zamboanga City http://t.co/4hUttNPI",
     "time_created" : 1357958119000,
     "version" : "v2.1"
},
{
     "_id" : { "oid" : "5213785fe4b0780ba56884d6" },
     "author" : "Clydepatrick Jayme ",
     "message" : "RT @iFootballPlanet: 74' Osasuna 0 - 0 Real Madrid\n\n-ASG.",
     "time_created" : 1358022721000,
     "version" : "v2.1"
}]
于 2013-11-02T23:16:15.440 回答