通过JSON获取数据

时间:2014-10-21 13:16:56

标签: javascript json

下面我有一些JSON,我想要做的是遍历JSON文件并拉出'title'元素并制作一个列表。有人可以帮忙吗?

{ "slides" : {
        "sl_0_0" : {
            "title" : "Slide 0-0",
            "copy" : "Copy for slide 0-0",
            "link" : "sl_0_0.html"
        },
        "sl_1_0" : {
            "title" : "Slide 1-0",
            "copy" : "Copy for slide 1-0",
            "link" : "sl_1_0.html"
        },
        "sl_1_1" : {
            "title" : "Slide 1-1",
            "copy" : "Copy for slide 1-1",
            "link" : "sl_1_1.html"
        },
        "sl_2_0" : {
            "title" : "Slide 2_0",
            "copy" : "Copy for slide 2_0",
            "link" : "sl_2_0.html"
        },
        "sl_2_1" : {
            "title" : "Slide 2_1",
            "copy" : "Copy for slide 2_1",
            "link" : "sl_2_1.html"
        },
        "sl_2_2" : {
            "title" : "Slide 2_2",
            "copy" : "Copy for slide 2_2",
            "link" : "sl_2_2.html"
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以像处理任何其他javascript对象一样处理JSON对象。所以,你可以使用像这样的for循环来进行交互:

var jsonObj = { /* your JSON object here */ };
for ( var id in jsonObj.slides ) {
    var slide = jsonObj.slides[id];
    var title = slide.title; //etc.
    /* Now you can build your list */
}