【问题标题】:How to read Json String which is coming in Key-Value format?如何读取键值格式的 Json 字符串?
【发布时间】:2012-01-03 17:55:22
【问题描述】:
我正在调用getTags web-method,它返回 JSON 格式的哈希表。
以下是由
格式化的返回值
jQuery.parseJSON(JSON.stringify(response))
并赋值给变量jsonObj
我想读取 Key 及其值。我该怎么做?
var jsonObj = {"getTags":[
{"Key":"TagID","Value":2},
{"Key":3,"Value":"Best College"}
]
}
【问题讨论】:
标签:
jquery
json
key-value
【解决方案1】:
var jsonObj = {"getTags":[{"Key":"TagID","Value":2},{"Key":3,"Value":"Best College"}]}
对于 ( 变量 i =0 ; 我
【解决方案2】:
您再次使用“键”和“值”作为键使 json 有点复杂。理想的 JSON 应该如下所示:
var jsonObj ={
"getTags": {
"3": "Best College",
"TagID": 2
}
}
使用下面的jquery来处理这个对象:
var items=[];
$.each(jsonObj, function(key, val) {
// you can do whatever you want with key value pair.
items.push(val);
});
console.log(items); // items contains ["Best College",2]
更多关于jQuery.com的帮助