使用多个密钥从JSON Feed访问数据

时间:2015-04-07 13:06:17

标签: php json

我有一个包含两个键或类别的JSON Feed。不知道该怎么称呼他们,但他们是"结果"和#34; PendedApps"。我尝试做的是创建两个表,其中包含来自两个提要的所有数据。下面是JSON提要的一个示例。

{ "Results": [ { "ClientFirst": "John", "ClientLast": "Doe", "ClientState": "CA", 
"Product": "Recliner", "RecDate": "4/1/2015", "Status": "Sold" }, { "ClientFirst": "Jay", 
"ClientLast": "Doe", "ClientState": "CA", "Product": "Leather Loveseat", "RecDate": 
"3/28/2015", "Status": "Completed" } ] }{ "PendedApps": [ { "ClientFirst": "Joe",
 "ClientLast": "Schmidt", "ClientState": "CA", "Product": "Wicker Stool", 
"PendReason": "Credit card declined\r\n" } ] }

以下是我在本网站上的其他示例中尝试过的一些内容:

我尝试通过foreach循环访问每个密钥。这将返回错误"为foreach()"

提供的无效参数
$content = json_decode($json);

foreach($content->Results->new as $entry){
    echo $entry->ClientFirst;
}

我还试过转储utf8_encoded数据并返回 NULL

$content = json_decode($json);

$newjson = utf8_encode($json); 
$newerjson = preg_replace('/.+?({.+}).+/','$1',$newjson);
$data = json_decode($newerjson);

var_dump($data);

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您的JSON无效,以下是更正:

{
    "Results": [
        {
            "ClientFirst": "John",
            "ClientLast": "Doe",
            "ClientState": "CA",
            "Product": "Recliner",
            "RecDate": "4/1/2015",
            "Status": "Sold"
        },
        {
            "ClientFirst": "Jay",
            "ClientLast": "Doe",
            "ClientState": "CA",
            "Product": "Leather Loveseat",
            "RecDate": "3/28/2015",
            "Status": "Completed"
        }
    ],
    "PendedApps": [
        {
            "ClientFirst": "Joe",
            "ClientLast": "Schmidt",
            "ClientState": "CA",
            "Product": "Wicker Stool",
            "PendReason": "Credit card declined\r\n"
        }
    ]
}