简体   繁体   中英

Read data from JSON file

I want to read the content of external JSON file and put in an array using javascript for further manipulations.

My JSON file is..

"Country":[
      {
         "Country_Name":"India",
         "Country_Details":[
            {
               "State_Name":"TamilNadu",
               "Capital":"Chennai",
               .................
               ................
            },
            {
               "State_Name":"Kerla",
               "Capital":"Trivandram",
               ................
               ................
            }
         ]
      },
      {
         "Country_Name":.......,
         "Country_Details":[
            {
              ...........
              .........
              .........
              .........
            }           
            {
              ........
              ........          
            }
          ]
         }    
       ]
      }

I have to use multi dimensional array (array inside array) i think.

I don't know to use push function on this array.

Suggest me something.Thanks in advance

http://jsoneditoronline.org/这个网站非常支持您将文件数据处理为 JSON。

Here is JavaScript snippet for parsing your JSON based example file.

 function httpGet(theUrl) //function for loading in JSON file from gist { var xmlHttp = new XMLHttpRequest(); xmlHttp.open( "GET", theUrl, false ); xmlHttp.send( null ); return xmlHttp.responseText; } var object = JSON.parse(httpGet("https://gist.githubusercontent.com/anonymous/4d342372ed151964bbc03bbad1b4db65/raw/d843d183522f1b16eea1cfc9f3e36c9f22ff5e05/Country.json")); //object storing JavaScript object turned from JSON file (your example) for (var i = 0;i<object.Country.length;i++) //for loop looping through array of objects stored in "var object" { console.log(object.Country[i].Country_Name) for (var j = 0;j<object.Country[i].Country_Details.length;j++){ console.log("Country Details - State: "+object.Country[i].Country_Details[j].State_Name) console.log("Country Details - Capital: "+object.Country[i].Country_Details[j].Capital) } }

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