JSON+Javascript/jQuery. How to import data from a json file and parse it?

前端 未结 7 2016
猫巷女王i
猫巷女王i 2021-01-31 19:12

If I have a JSON file named names.json with:

{\"employees\":[
    {\"firstName\":\"Anna\",\"lastName\":\"Meyers\"},
    {\"firstNam         


        
7条回答
  •  一个人的身影
    2021-01-31 19:55

    You can simply include a Javascript file in your HTML that declares your JSON object as a variable. Then you can access your JSON data from your global Javascript scope using data.employees, for example.

    index.html:

    
    
    
    
      
    
    
    

    data.js:

    var data = {
      "employees": [{
        "firstName": "Anna",
        "lastName": "Meyers"
      }, {
        "firstName": "Betty",
        "lastName": "Layers"
      }, {
        "firstName": "Carl",
        "lastName": "Louis"
      }]
    }
    

提交回复
热议问题