How to get json key and value in javascript?
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..
Solution 1:
//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/
Solution 2:
you have parse that Json
string using JSON.parse()
..
}).done(function(data){
obj = JSON.parse(data);
alert(obj.jobtitel);
});
Solution 3:
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);
Solution 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);
})();
Solution 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]);
}