Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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..

like image 549
doniyor Avatar asked Sep 20 '13 07:09

doniyor


People also ask

How do I get the key of a JSON object?

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.

What is key and value in JSON?

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 ( : ).

Can we get key from value in JavaScript?

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.


1 Answers

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

like image 83
swan Avatar answered Sep 19 '22 09:09

swan