99

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']);
});

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

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
doniyor
  • 36,596
  • 57
  • 175
  • 260

8 Answers8

165
//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

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

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

sawan
  • 2,341
  • 3
  • 25
  • 51
  • 2
    This is not documented enough. The ability to use json['KeyAsString']. Really great. Thank you. – acrawly Mar 24 '14 at 20:09
  • How would I resolve Entwickler to jobtitel? Is there anythind native or do I have to write my own code? – dGRAMOP Jun 18 '17 at 03:31
24

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

..
}).done(function(data){
    obj = JSON.parse(data);
    alert(obj.jobtitel);
});
NaYaN
  • 1,300
  • 7
  • 11
13
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);
onihime
  • 129
  • 4
5

For getting key

var a = {"a":"1","b":"2"};
var keys = []
for(var k in a){
  keys.push(k)
}

For getting value.

var a = {"a":"1","b":"2"};
var values = []
for(var k in a){
  values.push(a[k]);
}
Chandan Kumar
  • 1,066
  • 10
  • 16
4

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);
})();
3

A simple approach instead of using JSON.parse

 success: function(response){
     var resdata = response;
     alert(resdata['name']);
}
knoxgon
  • 1,070
  • 2
  • 15
  • 31
ricky
  • 41
  • 4
2

It looks like data not contains what you think it contains - check it.

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

console.log( data["jobtitel"] );
console.log( data.jobtitel );
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
0

You can use the following solution to get a JSON key and value in JavaScript:

var dt = JSON.stringify(data).replace('[', '').replace(']', '');
if (dt) {
  var result = jQuery.parseJSON(dt);
  var val = result.YOUR_OBJECT_NAME;
}
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
Monzur
  • 1,341
  • 14
  • 11
  • 1
    While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Narendra Jadhav Aug 27 '18 at 13:42