Giridharan Giridharan - 2 months ago 6
jQuery Question

How to get map variable of Json using Jquery

I am stuck with an issue of parsing data from an array using jquery. Kindly guide me to fix the issue.

var json = '{ "menu1": ["City1", "City2", "City3"], "menu2": ["City1", "City2"] } ';
var arr = $.parseJSON(json);
console.log(arr);


output:
undefined

Expected ouput:

Menu1: ["City1","City2","City3"]

Menu2: ["City1","City2"]


How do I parse the values for the key
menu1
from the above
json
?

Answer

use the . operator to access the fields after parssing json

var json = '{ "menu1": ["City1", "City2", "City3"], "menu2": ["City1", "City2"] } ';
var arr = $.parseJSON(json);
console.log(arr.menu1);
console.log(arr.menu2);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>