我不是很喜欢JavaScript和JSON我对应用程序有以下疑问。这个JavaScript JSON“操纵”到底是什么?
我有这个JSON文件:
{
"Markets": {
"Market": {
"market_name": "Kibuye",
"market_description": "Kibuye Village Market",
"localization_id": 1,
"long": 29.371491,
"lat": -2.17897,
"country": "Rwanda",
"regione": {
"@nil": "true"
},
"province": {
"@nil": "true"
},
"city": {
"@nil": "true"
},
"district": {
"@nil": "true"
},
"town": {
"@nil": "true"
},
"village": {
"@nil": "true"
},
"commodity": {
"el": [{
"market_name": "Kibuye",
"commodity_name": "Rice-Asia",
"image_link": "Rice-Asia.jpg",
"commodity_details_id": 1
}, {
"market_name": "Kibuye",
"commodity_name": "Apple banana",
"image_link": "Apple_banana.jpg",
"commodity_details_id": 2
}, {
"market_name": "Kibuye",
"commodity_name": "Avocado",
"image_link": "Avocado.jpg",
"commodity_details_id": 3
}, {
"market_name": "Kibuye",
"commodity_name": "Red onion",
"image_link": "Red_onion.jpg",
"commodity_details_id": 4
}, {
"market_name": "Kibuye",
"commodity_name": "White onion",
"image_link": "White_onion.jpg",
"commodity_details_id": 5
}, {
"market_name": "Kibuye",
"commodity_name": "Green Beans",
"image_link": "Green_Beans.jpg",
"commodity_details_id": 6
}, {
"market_name": "Kibuye",
"commodity_name": "Bell pepper",
"image_link": "Bell_pepper.jpg",
"commodity_details_id": 7
}, {
"market_name": "Kibuye",
"commodity_name": "Carrot",
"image_link": "Carrot.jpg",
"commodity_details_id": 8
}, {
"market_name": "Kibuye",
"commodity_name": "Tomatoes",
"image_link": "Tomatoes.jpg",
"commodity_details_id": 9
}, {
"market_name": "Kibuye",
"commodity_name": "Irish potatoes",
"image_link": "Irish_potatoes.jpg",
"commodity_details_id": 10
}, {
"market_name": "Kibuye",
"commodity_name": "Maize",
"image_link": "Maize.jpg",
"commodity_details_id": 11
}
]
}
}
}
}
然后进入我的JavaScript代码,我有这样的事情:
var payload = JSON.parse(pl_string);
// create new response
var response = payload.Markets.Market;
凡pl_string参数包含以前的JSON文件。
第二条指令究竟干什么?
我认为把这个JSON节到响应变量(市场的内容 - >市场领域:
"market_name": "Kibuye",
"market_description": "Kibuye Village Market",
"localization_id": 1,
"long": 29.371491,
"lat": -2.17897,
"country": "Rwanda",
"regione": {
"@nil": "true"
},
"province": {
"@nil": "true"
},
"city": {
"@nil": "true"
},
"district": {
"@nil": "true"
},
"town": {
"@nil": "true"
},
"village": {
"@nil": "true"
},
"commodity": {
"el": [{
"market_name": "Kibuye",
"commodity_name": "Rice-Asia",
"image_link": "Rice-Asia.jpg",
"commodity_details_id": 1
}, {
"market_name": "Kibuye",
"commodity_name": "Apple banana",
"image_link": "Apple_banana.jpg",
"commodity_details_id": 2
}, {
"market_name": "Kibuye",
"commodity_name": "Avocado",
"image_link": "Avocado.jpg",
"commodity_details_id": 3
}, {
"market_name": "Kibuye",
"commodity_name": "Red onion",
"image_link": "Red_onion.jpg",
"commodity_details_id": 4
}, {
"market_name": "Kibuye",
"commodity_name": "White onion",
"image_link": "White_onion.jpg",
"commodity_details_id": 5
}, {
"market_name": "Kibuye",
"commodity_name": "Green Beans",
"image_link": "Green_Beans.jpg",
"commodity_details_id": 6
}, {
"market_name": "Kibuye",
"commodity_name": "Bell pepper",
"image_link": "Bell_pepper.jpg",
"commodity_details_id": 7
}, {
"market_name": "Kibuye",
"commodity_name": "Carrot",
"image_link": "Carrot.jpg",
"commodity_details_id": 8
}, {
"market_name": "Kibuye",
"commodity_name": "Tomatoes",
"image_link": "Tomatoes.jpg",
"commodity_details_id": 9
}, {
"market_name": "Kibuye",
"commodity_name": "Irish potatoes",
"image_link": "Irish_potatoes.jpg",
"commodity_details_id": 10
}, {
"market_name": "Kibuye",
"commodity_name": "Maize",
"image_link": "Maize.jpg",
"commodity_details_id": 11
}
]
难道还是我失去了一些东西
呀,漂亮多了。 – deceze
如果你在代码中放置了一个调试器,你会发现基本上你可以“导航”JSON并根据你需要的响应的哪一部分创建变量 – Anon
因此,如何调试代码?只要运行它,试试看,看看会发生什么。我有点困惑,为什么你作为一个10k的用户,没有显示出实际上只是尝试代码的迹象... – Cerbrus