使用PHP解析字符串中的问题

时间:2016-01-15 08:26:46

标签: php json

以下是我收到的回复。

{  
    "StartTime":"2016-01-15T03:22:33.109787-05:00",
    "EndTime":"2016-01-15T03:22:34.2504378-05:00",
    "queryController":{  
        "TotalRecords":8355,
        "PageNumber":1,
        "PageSize":8,
        "DataFilters":[  

        ],
        "PropertyListing":[  
            {  
                "EntityName":"Product",
                "PropertyName":"IXOneId"
            },
            {  
                "EntityName":null,
                "PropertyName":"UPC12"
            }
        ],
        "EntityName":"QueryController"
    },
    "Products":[  
        {  
            "EntityName":"Product",
            "IXOneId":"SNL1080",
            "UPC12":"037014000245"
        },
        {  
            "EntityName":"Product",
            "IXOneId":"SNL1090",
            "UPC12":"747599617003"
        },
        {  
            "EntityName":"Product",
            "IXOneId":"SNL1079",
            "UPC12":"024182001822"
        },
        {  
            "EntityName":"Product",
            "IXOneId":"SNL1102",
            "UPC12":"745158300519"
        },
        {  
            "EntityName":"Product",
            "IXOneId":"SNL1077",
            "UPC12":"024182001891"
        },
        {  
            "EntityName":"Product",
            "IXOneId":"SNL1148",
            "UPC12":"039978003645"
        },
        {  
            "EntityName":"Product",
            "IXOneId":"SNL1110",
            "UPC12":"070670005759"
        },
        {  
            "EntityName":"Product",
            "IXOneId":"SNL1083",
            "UPC12":"037014000290"
        }
    ],
    "ResponseMessages":null,
    "EntityName":"ProductSearchReturn"
}

我想获得 IXOneId &的值 UPC12 所有记录的值,并希望在我的页面上显示。如何在PHP的帮助下获得这个?有点坚持下去。将不胜感激。

3 个答案:

答案 0 :(得分:1)

假设以下变量$string包含您的样本json字符串 使用json解码解码并迭代内部Products数组。

$json = json_decode($string);

foreach($json->Products as $product){
    print $product->IXOneId . ' ' . $product->UPC12 . PHP_EOL;
}

将输出

SNL1080 037014000245
SNL1090 747599617003
SNL1079 024182001822
SNL1102 745158300519
SNL1077 024182001891
SNL1148 039978003645
SNL1110 070670005759
SNL1083 037014000290

答案 1 :(得分:0)

这是一个JSON结构。使用json_decode将其转换为数组,然后使用while循环或for循环对其进行循环,以便按照您需要的方式提取数据。

答案 2 :(得分:0)

将数据存储在变量中,此处为$ arr,然后使用json_decode。 然后你可以使用foreach

 $arr=json_decode($arr);
 print_r($arr->Products);