无法将 JSON 字符串解析为数组


Can't get JSON string to parse into an array

我使用以下PHP代码返回一个json字符串:

$itemURL = 'http://***.***.***/search?tag=Football&affiliate_id=&max_results=3';
$response = file_get_contents($itemURL);//curl_exec($curlHandle);
echo $response;
$response = array($response);
echo $response[0];

我得到一个看起来像这样的 json 字符串:

[ { "ID": "123", "描述":"冠军足球海军T恤", "高价": 16.99, "低价": 16.99, "销售价格":空, "打折":假, "网址": "http://www.mystore.com/itemDescription", "图像网址": "http://www.mystore.com/mainstore/045.jpg", "LargeImageURL": "http://www.mystore.com/mainstore/045.jpg", "缩略图URL": "http://www.mystore.com/mainstore/045.jpg", "MiniImageURL": "http://www.mystore.com/mainstore/045.jpg", "会员 ID": " }, { "ID": "23", "描述": "冠军足球海军 T 恤 XL", "高价": 16.99, "低价": 16.99, "销售价格":空, "打折":假, "网址": "http://www.mystore.com/itemDescription", "图像网址": "http://www.mystore.com/mainstore/045.jpg", "LargeImageURL": "http://www.mystore.com/mainstore/045.jpg", "缩略图URL": "http://www.mystore.com/mainstore/045.jpg", "MiniImageURL": "http://www.mystore.com/mainstore/045.jpg", "会员 ID": " } ]

但是,当我回显$response[0]时,我得到了整个字符串。如果我使用 json_decode 或编码,我会得到一个字符串,但周围有引号。我不知道如何投射这个 dang 的东西,所以它作为一个对象数组运行,然后我可以迭代。提前感谢您的任何帮助

试试这个...

$itemURL = 'http://***.***.***/search?tag=Football&affiliate_id=&max_results=3';
$response = file_get_contents($itemURL);//curl_exec($curlHandle);
$response = json_decode($response);

你会得到一个像...

array(2) {
  [0]=>
  object(stdClass)#1 (12) {
    ["ID"]=>
    string(3) "123"
    ["Description"]=>
    string(30) "Champion Football Navy T-shirt"
    ["HighPrice"]=>
    float(16.99)
    ["LowPrice"]=>
    float(16.99)
    ["SalePrice"]=>
    NULL
    ["OnSale"]=>
    bool(false)
    ["URL"]=>
    string(38) "http://www.mystore.com/itemDescription"
    ["ImageURL"]=>
    string(40) "http://www.mystore.com/mainstore/045.jpg"
    ["LargeImageURL"]=>
    string(40) "http://www.mystore.com/mainstore/045.jpg"
    ["ThumbnailImageURL"]=>
    string(40) "http://www.mystore.com/mainstore/045.jpg"
    ["MiniImageURL"]=>
    string(40) "http://www.mystore.com/mainstore/045.jpg"
    ["AffiliateID"]=>
    string(0) ""
  }
  [1]=>
  object(stdClass)#2 (12) {
    ["ID"]=>
    string(2) "23"
    ["Description"]=>
    string(33) "Champion Football Navy T-shirt XL"
    ["HighPrice"]=>
    float(16.99)
    ["LowPrice"]=>
    float(16.99)
    ["SalePrice"]=>
    NULL
    ["OnSale"]=>
    bool(false)
    ["URL"]=>
    string(38) "http://www.mystore.com/itemDescription"
    ["ImageURL"]=>
    string(40) "http://www.mystore.com/mainstore/045.jpg"
    ["LargeImageURL"]=>
    string(40) "http://www.mystore.com/mainstore/045.jpg"
    ["ThumbnailImageURL"]=>
    string(40) "http://www.mystore.com/mainstore/045.jpg"
    ["MiniImageURL"]=>
    string(40) "http://www.mystore.com/mainstore/045.jpg"
    ["AffiliateID"]=>
    string(0) ""
  }
}

然后你可以使用 $response[0]、$response[1] 等来访问你的 JSON 对象。使用字段的特定名称,如 $response[0]->关联 ID

您需要调用您要查找的属性

即:

echo response[0].ID; //getting the 1st item in the response array then access the "ID" property of the json object.

json 响应是一个动态对象,允许您与所有属性进行交互,就像从头开始创建对象一样。

[{...和 ... }]是对象值。

所以。。。

var json = '{"ID" : 1, "Prop1" : "Value1", Prop2 : "Value2" }';

可以使用jQuery的parseJSON方法解析

var obj= jQuery.parseJSON(json);
echo obj.ID; //1
echo obj.Prop1; //Value
echo obj.Prop2; //Value2

当您有 [...和...]在 { 和 } 周围,您知道此对象是一个数组,需要相应地处理。

希望这有帮助。

威姆

if (mysql_num_rows($result) > 0) {
$response["events"] = array();
while ($row = mysql_fetch_array($result)) {
      $tmparr = array();
        $tmparr["uid"] = $row["uid"];
        $tmparr["date"] = $row["date"];
        $tmparr["hours"] = $row["hours"];
        $tmparr["store_name"] = $row["store_name"];
        $tmparr["event_information"] = $row["event_information"];
        $tmparr["event_type"] = $row["event_type"];
        $tmparr["Phone"] = $row["Phone"];
        $tmparr["price"] = $row["price"];
        $tmparr["address"] = $row["address"];
        $tmparr["image_url"] = $row["image_url"];
        $tmparr["created_at"] = $row["created_at"];
        $tmparr["updated_at"] = $row["updated_at"];
        array_push($response["events"], $tmparr);
}    
$response["success"] = 1;
echo json_encode($response);

我不明白你想做什么,但这是我用来从 sql 查询 ($result) 返回数据行数组的方式。我希望这对你有帮助