jquery parsing json
jquery parsing json
这是我目前拥有的JSON输出:
[{"pk": 1, "model": "system.employees",
"fields": {"chi_name": "N/A", "eng_name": "Eli"}}]
我希望输出为
[{"label": "Eli", "value": "1"}]
如何从JSON数据中获取pk和eng_name的值并像上面那样输出?
您可以使用jQuery.map:
var data = [{"pk": 1, "model": "system.employees",
"fields": {"chi_name": "N/A", "eng_name": "Eli"}}];
var new = $.map(data, function(index, item) {
return { label: item.fields.eng_name, value: item.pk };
});
var result = [{"pk": 1, "model": "system.employees", "fields": {"chi_name": "N/A", "eng_name": "Eli"}}]
var output = [{ "label" : result[0].fields.eng_name, "value": result[0].pk}]
//assuming your source obj is called 'source'
var num = source[0].pk;
var eng_name = source[0].fields.eng_name;
然后你可以用它们做任何事情,比如
var output = [];
output.push({"label":eng_name, "value":num});
祝你好运!
Try-
var h = JSON.parse('[{"pk": 1, "model": "system.employees", "fields": {"chi_name": "N/A", "eng_name": "Eli"}}]');
var a = [];
a.push({"label": h[0].fields.eng_name, "value": h[0].pk+''})
alert(JSON.stringify(a))
注意您需要导入此代码-https://github.com/douglascrockford/JSON-js/blob/master/json2.js如果您的浏览器不支持JSON.parse
和JSON.stringify
演示-http://jsfiddle.net/ipr101/uwZVW/
相关文章:
- jQuery匹配JSON对象的部分文本
- 在循环中分配json值时,值被覆盖
- 需要帮助设置json数组
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 使用JQuery解析JSON嵌套数组
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- Json javascript parsing
- MongoDB - JSON parsing
- Go vs JavaScript JSON parsing
- Parsing JSON stream
- jquery parsing json
- Xcode Javascript JSON parsing
- JavaScript: parsing JSON
- Parsing JSON with Javascript
- javascript JSON parsing
- Json Parsing html
- Angularjs/Ionic App json parsing