我不是很喜歡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