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..
To get key and value from json object in javascript, you can use Object. keys() , Object. values() , for Object. entries() method the methods helps you to get both key and value from json object.
A JSON object contains zero, one, or more key-value pairs, also called properties. The object is surrounded by curly braces {} . Every key-value pair is separated by a comma. The order of the key-value pair is irrelevant. A key-value pair consists of a key and a value, separated by a colon ( : ).
To get an object's key by it's value:Use the find() method to find the key that corresponds to the value. The find method will return the first key that satisfies the condition.
//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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With