简体   繁体   中英

Nested json data in js -get the values from nested array

Got the output result in json nested array.

Help to access the USER-id of this json format .

var result = {
    "USER": {
        "id": "11456",
        "email": "g@gmail.com",
        "name": "g"
    },
    "status": "true",
    "group-title": "title",
    "group-name": "2-Group"
}

I thin your json structue is wrong. Below is the corrected structure

{
  "status": "true",
  "USER": {
    "id": "11456",
    "name": "g",
    "email": "g@‌​gmail.com"
  },
  "group-name": "2-Group",
  "group-title": "title"
}

The json usages in JS

 var result={  "status": "true",  "USER": {    "id": "11456",    "name": "g",    "email": "g@‌​gmail.com"  },  "group-name": "2-Group",  "group-title": "title"};
resultJson=jQuery.parseJSON(result);

var userId=resultJson.USER.id; // here you will get the user id

Please try this way. This may help you. Don't forget to add jQuery in your script

json values are not seperated by

 ;

the correct formate for your json is

var result =  {"status":"true","USER":{"id":"11456","name":"g","email":"g@gmail.com"},
                "group-name":"2-Group","group-title":"title"}

for this formate u can assess the user id by

    result.USER.id;
From your code, you can directly access Id as below:

<script>
var result = {
    "USER": {
        "id": "11456",
        "email": "g@gmail.com",
        "name": "g"
    },
    "status": "true",
    "group-title": "title",
    "group-name": "2-Group"
};

alert(result.USER.id);
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM