访问json对象和数组 [英] access json object and array
本文介绍了访问json对象和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的JSON数据:
Here is my JSON data :
{
"Liste_des_produits1": [{
"Added_Time": "28-Sep-2009 16:35:03",
"prod_ingredient": "sgsdgds",
"prod_danger": ["sans danger pour xyz"],
"prod_odeur": ["Orange"],
"prod_nom": "ceciestunproduit",
"prod_certification": ["HE • Haute Efficité", "Certifier Ecologo", "Contenant recyclable"],
"prod_photo": "",
"prod_categorie": ["Corporel"],
"prod_desc": "gdsg",
"prod_format": ["10 kg", "20 kg"]
}, {
"Added_Time": "28-Sep-2009 16:34:52",
"prod_ingredient": "dsgdsgdsf",
"prod_danger": ["Sans danger pour le fausse sceptiques"],
"prod_odeur": ["Agrumes", "Canneberge"],
"prod_nom": "jsute un test",
"prod_certification": ["100% Éco-technologie", "Certifier Ecologo", "Contenant recyclable"],
"prod_photo": "",
"prod_categorie": ["Corporel"],
"prod_desc": "gsdgsdgsdg",
"prod_format": ["1 Litre", "10 kg"]
}]
}
在PHP中,访问不同数据值的方式是什么?
In PHP, what is the way to access different values of data?
例如:prod_ingredient
或prod_danger
.
我尝试过:
$prod = $result->{'Liste_des_produits1'};
print_r($prod[0]); // error
和
$result = json_decode($json);
print_r($result['Liste_des_produits1'].prod_ingredient[1]); // error
推荐答案
使用 json_decode 将数据转换为关联数组.
Use json_decode to convert the data to an associative array.
$data = json_decode($jsonString, true);
// extend this concept to access other values
$prod_ingredient = $prod['Liste_des_produits1'][0]['prod_ingredient'];
这篇关于访问json对象和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文