简体   繁体   English

转换JSON文件 <ul><li> 使用纯Javascript

[英]Convert JSON file in <ul><li> using plain Javascript

I have JSON file that contains : 我有包含的JSON文件:

{
    "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)."
},

Im need to somehow display it as "ul" "li" list by using plain javascript I wrote this code but it doesn't work: 我需要通过使用我编写此代码的纯javascript将其显示为“ ul”“ li”列表,但它不起作用:

function createList(){
    var arr = JSON.parse(phones);
    var out = "<ul>";
    for(var i = 0; i < arr.length;i++){
        out+="<li>" + arr[i].age + arr.id[i]+
        arr[i].imageUrl + arr[i].name + arr[i].snippet + "</li>";
    }

    out+= "</ul>";
    document.getElementById("div1").innerHTML = out;
}

Phones needs to be an array and you had a syntax error on one of the array derefs: 电话必须是一个数组,并且其中一个数组的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;
    } 

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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