admin管理员组文章数量:1277887
I have a json payload like below;
{ "ABW": "Aruba", "AFG": "Afghanistan", "AGO": "Angola" }
As you see, it will have all countries. I can read a JSON payload as it is mentioned here;
/
One problem here is that, how am I going to read the Country Codes here? They will change for every each value.
My goal here is to assign the Country code to value property of the option and country name to text of the option in a select list.
I have a json payload like below;
{ "ABW": "Aruba", "AFG": "Afghanistan", "AGO": "Angola" }
As you see, it will have all countries. I can read a JSON payload as it is mentioned here;
http://webhole/2009/11/28/how-to-read-json-with-javascript/
One problem here is that, how am I going to read the Country Codes here? They will change for every each value.
My goal here is to assign the Country code to value property of the option and country name to text of the option in a select list.
Share Improve this question edited Jun 22, 2011 at 16:25 tugberk asked Jun 22, 2011 at 16:17 tugberktugberk 58.5k69 gold badges251 silver badges342 bronze badges 5- 2 Just to save you some debugging time: The required delimiter for all strings (including key names) is ", not “. As quoted, that JSON is invalid, so if you were copying and pasting from your actual source... – T.J. Crowder Commented Jun 22, 2011 at 16:21
-
@TJCrowder, i had that issue when i was trying to make the fiddle. I had to convert all the
"
– Naftali Commented Jun 22, 2011 at 16:22 - Why not display format your json like this [{"Code":"ABW","Name":"Aruba"},{"Code":"AFG","Name":"Afghanistan"}] – Amin Eshaq Commented Jun 22, 2011 at 16:23
- @Amin -- see ment to @RobotWoods' answer... – Naftali Commented Jun 22, 2011 at 16:26
- 1 @TJCrowder nice touch there. thanks ! – tugberk Commented Jun 22, 2011 at 16:26
4 Answers
Reset to default 9No need for any jQuery. Good ol' plain javascript to the rescue:
var countries = { "ABW": "Aruba", "AFG": "Afghanistan", "AGO": "Angola" };
for(code in countries){
alert("code: " + code + "\n" + "country: " + countries[code]);
}
Fiddle: http://jsfiddle/maniator/2adKZ/
When JQuery uses the each function the index is made up of the name of the first bit of information So in { "ABW": "Aruba", "AFG": "Afghanistan", "AGO": "Angola" } the Index for the first object will be "ABW" and the value "Aruba"
var countries = { "ABW": "Aruba", "AFG": "Afghanistan", "AGO": "Angola" }
$.each( countries , function(index) {
var code = index;
var country = countries[index]
})
or
$.each( countries , function(index) {
var code = index;
var country = this;
})
Can you change the response structure? to (maybe):
{"countries":[{"code":"ABW","name":"Aruba"},{"code:"AFG","name":"Afghanistan"}]}
Why not just make it an an array of objects, and then loop through each?
{"results" : [
{countryCode : "ABW",
country : "Aruba"},
{countryCode : "AFG",
country : "Afghanistan"}]
Then loop through each object in your array to get the country code and country name.
本文标签: javascriptReading JSON with JQueryField NamesStack Overflow
版权声明:本文标题:javascript - Reading JSON with JQuery : Field Names - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741267489a2368755.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论