如何在javascript中获取json键和值? [英] how to get json key and value in javascript?
本文介绍了如何在javascript中获取json键和值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将返回一个json,如下所示
I am returning a json as shown below
{"name": "", "skills": "", "jobtitel": "Entwickler", "res_linkedin": "GwebSearch"}
我想要获得每个元素键和值:
I am trying to get each element key and value:
..
}).done(function(data){
alert(data['jobtitel']);
});
我在警报中收到 undefined
。为什么?我试过 data.jobtitel
,我试过循环但没有成功..
I am getting undefined
in alert. WHY? I tried data.jobtitel
, i tried loop but no success..
推荐答案
//By using jquery json parser
var obj = $.parseJSON('{"name": "", "skills": "", "jobtitel": "Entwickler", "res_linkedin": "GwebSearch"}');
alert(obj['jobtitel']);
//By using javasript json parser
var t = JSON.parse('{"name": "", "skills": "", "jobtitel": "Entwickler", "res_linkedin": "GwebSearch"}');
alert(t['jobtitel'])
从开始jQuery 3.0 ,$ .parseJSON已弃用。要解析JSON字符串,请使用
本机JSON.parse方法。
As of jQuery 3.0, $.parseJSON is deprecated. To parse JSON strings use the native JSON.parse method instead.
来源: http://api.jquery.com/jquery.parsejson/
这篇关于如何在javascript中获取json键和值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文