-1
var json ='{
 "maps":{

  "map":[
     {
        "name":"acb bank",
        "langName":"acb bank",
        "address":"distric 7",
        "coordinate":"10.041285568128918,105.79299676260861"
     },
     {
        "name":"food store",
        "langName":"vietnam food",
        "address":"distric 8",
        "coordinate":"10.06196571359739,105.78313580009822"
     }
  ]
 }
}';

var getdata = JSON.stringify(json);

for(var i=0; i < getdata.length; i++){
    document.write(getdata.maps.map[i].name);
}

i can't get name or coordinate...Please help me parse to it.

SheetJS
  • 22,470
  • 12
  • 65
  • 75
Tan Nguyen
  • 21
  • 2

2 Answers2

0

To Parse JSON in JS you can use JSON.parse.

You are probably looking for something like this

var json ={
 "maps":{
     "map":[
         {
            "name":"acb bank",
            "langName":"acb bank",
            "address":"distric 7",
            "coordinate":"10.041285568128918,105.79299676260861"
         },
         {
            "name":"food store",
            "langName":"vietnam food",
            "address":"distric 8",
            "coordinate":"10.06196571359739,105.78313580009822"
         }
      ]
     }
    };

     var data = json.maps.map;
     for(var i = 0, len = data.length; i < len; i++) 
         document.write(data[i].name);
CBIII
  • 845
  • 1
  • 9
  • 9
0

Remove the line breaks in your string. Also, remove the comma in coordinate. Also, use:

JSON.parse(json);

Edit: Actually, it seems like you actually wanted to know how to properly stringify an object into json. In that case, remove the two single quotes at the start and end of your object.

Jackson
  • 9,188
  • 6
  • 52
  • 77