如何在javascript中获取json键和值?

时间:2022-08-23 08:10:04

I am returning a json as shown below

我返回一个json,如下所示

{"name": "", "skills": "", "jobtitel": "Entwickler", "res_linkedin": "GwebSearch"}

I am trying to get each element key and value:

我正在尝试获取每个元素键和值:

..
}).done(function(data){
    alert(data['jobtitel']);
});

I am getting undefined in alert. WHY? I tried data.jobtitel, i tried loop but no success..

我变得不清楚了。为什么?我试着数据。jobtitel,我尝试了loop但是没有成功。

5 个解决方案

#1


105  

//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'])

Check this jsfiddle

检查这个jsfiddle

As of jQuery 3.0, $.parseJSON is deprecated. To parse JSON strings use the native JSON.parse method instead.

从jQuery 3.0开始,$。parseJSON弃用。要解析JSON字符串,请使用本机JSON。解析方法。

Source: http://api.jquery.com/jquery.parsejson/

来源:http://api.jquery.com/jquery.parsejson/

#2


14  

you have parse that Json string using JSON.parse()

使用Json .parse()解析Json字符串

..
}).done(function(data){
    obj = JSON.parse(data);
    alert(obj.jobtitel);
});

#3


6  

var data = {"name": "", "skills": "", "jobtitel": "Entwickler", "res_linkedin": "GwebSearch"}

var parsedData = JSON.parse(data);
alert(parsedData.name);
alert(parsedData.skills);
alert(parsedData.jobtitel);
alert(parsedData.res_linkedin);

#4


3  

http://jsfiddle.net/v8aWF/

http://jsfiddle.net/v8aWF/

Worked out a fiddle. Do check it out

一个小提琴。检查出来

(function() {
    var oJson = {
        "name": "", 
        "skills": "", 
        "jobtitle": "Entwickler", 
        "res_linkedin": "GwebSearch"
    }
    alert(oJson.jobtitle);
})();

#5


2  

A simple approach instead of using JSON.parse

一种简单的方法,而不是使用JSON.parse

 success: function(response){
     var resdata = response;
     alert(resdata['name']);
}

#1


105  

//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'])

Check this jsfiddle

检查这个jsfiddle

As of jQuery 3.0, $.parseJSON is deprecated. To parse JSON strings use the native JSON.parse method instead.

从jQuery 3.0开始,$。parseJSON弃用。要解析JSON字符串,请使用本机JSON。解析方法。

Source: http://api.jquery.com/jquery.parsejson/

来源:http://api.jquery.com/jquery.parsejson/

#2


14  

you have parse that Json string using JSON.parse()

使用Json .parse()解析Json字符串

..
}).done(function(data){
    obj = JSON.parse(data);
    alert(obj.jobtitel);
});

#3


6  

var data = {"name": "", "skills": "", "jobtitel": "Entwickler", "res_linkedin": "GwebSearch"}

var parsedData = JSON.parse(data);
alert(parsedData.name);
alert(parsedData.skills);
alert(parsedData.jobtitel);
alert(parsedData.res_linkedin);

#4


3  

http://jsfiddle.net/v8aWF/

http://jsfiddle.net/v8aWF/

Worked out a fiddle. Do check it out

一个小提琴。检查出来

(function() {
    var oJson = {
        "name": "", 
        "skills": "", 
        "jobtitle": "Entwickler", 
        "res_linkedin": "GwebSearch"
    }
    alert(oJson.jobtitle);
})();

#5


2  

A simple approach instead of using JSON.parse

一种简单的方法,而不是使用JSON.parse

 success: function(response){
     var resdata = response;
     alert(resdata['name']);
}