我需要从JSON字符串中获取值。这是字符串:
[{
"title": "T-shirt femme",
"thumbnail": "http://www.donquishirt.fr/images/tshirtdesigns/thumbnail/1441979243_TMB_t-shirt-femme-vignette.png",
"elements": [{
"title": "Shirt",
"source": "http://www.donquishirt.fr/images/tshirtdesigns/front/1439370862_womens_classic_basic.png",
"parameters": {
"x": 225,
"y": 340,
"z": -1,
"opacity": 1,
"originX": "center",
"originY": "center",
"scale": 1,
"degree": 0,
"price": 15,
"colors": ["#f2f2f2", "#4ec1de", "#f05a9a", "#343434", "#f1730c", "#facde2"],
"currentColor": false,
"removable": false,
"draggable": false,
"rotatable": false,
"resizable": false,
"zChangeable": false,
"boundingBox": false,
"autoCenter": false,
"replace": "",
"boundingBoxClipping": false,
"autoSelect": true,
"topped": false,
"flipX": false,
"flipY": false,
"colorPrices": {},
"elementID": 27,
"uploadZone": false,
"filter": false,
"filters": [],
"isInitial": true,
"source": "http://www.donquishirt.fr/images/tshirtdesigns/front/1439370862_womens_classic_basic.png"
},
"type": "image"
}]
}, {
"title": "T-shirt femme (back)",
"thumbnail": "http://www.donquishirt.fr/images/tshirtdesigns/back/1443879426_t-shirt-femme-dos.png",
"elements": []
}
我需要从" elementID"中捕获值。它嵌套在"元素/参数"
中我该怎么办?感谢
答案 0 :(得分:1)
顶级数据是一个数组,因此您需要对其进行索引。然后elements
包含一个数组,因此您也必须将其编入索引。
data[0].elements[0].parameters.elementID
如果有多个顶级项目和元素,则需要使用嵌套循环来处理和访问。
data[i].elements[j].parameters.elementID
通常,解析JSON很简单。当您在JSON中看到[
时,它就是一个数组,并使用[number]
对其进行索引。当您看到{
时,它就是一个对象,您可以使用.propname
访问属性。