Convert JSON file in
  • using plain Javascript

前端 未结 2 1129
天涯浪人
天涯浪人 2021-01-26 13:33

I have JSON file that contains :

{
    \"age\": 0,
    \"id\": \"motorola-xoom-with-wi-fi\",
    \"imageUrl\": \"img/phones/motorola-xoom-with-wi-fi.0.jpg\",
            


        
相关标签:
2条回答
  • 2021-01-26 14:21

    Phones needs to be an array and you had a syntax error on one of the array derefs:

    var phones = [{
            "age": 0,
            "id": "motorola-xoom-with-wi-fi",
            "imageUrl": "img/phones/motorola-xoom-with-wi-fi.0.jpg",
            "name": "Motorola XOOM\u2122 with Wi-Fi",
            "snippet": "The Next, Next Generation\r\n\r\nExperience the future with Motorola XOOM with Wi-Fi, the world's first tablet powered by Android 3.0 (Honeycomb)."
        }];
    
        function createList(){
            var arr = JSON.parse(phones);
            var out = "<ul>";
            for(var i = 0; i < arr.length;i++){
                out+="<li>" + arr[i].age + arr[i].id+
                arr[i].imageUrl + arr[i].name + arr[i].snippet + "</li>";
            }
    
            out+= "</ul>";
            console.log(out);
            document.getElementById("div1").innerHTML = out;
        } 
    
    0 讨论(0)
  • 2021-01-26 14:30

    try this

    var phones = {"age" : "0",  "id" : "motorola-xoom-with-wi-fi",  "imageUrl" : "img/phones/motorola-xoom-with-wi-fi.0.jpg",   "name" : "Motorola XOOM\u2122 with Wi-Fi",  "snippet" : "The Next Next Generation Experience the future with Motorola XOOM with Wi-Fi, the worlds first tablet powered by Android 3.0 (Honeycomb)."};
    
       var arr = phones;
       console.log(arr);
       var out = "<ul><li> age : " + arr.age + "</li><br><li> id : " + arr.id + "</li><br><img src='" + arr.imageUrl + "'/></li><br><li> name : " + arr.name + "</li><br><li> snippet : " + arr.snippet + "</li>"
       document.getElementById("div1").innerHTML = out;
    
    0 讨论(0)
提交回复
热议问题