【问题标题】:read JSON from file从文件中读取 JSON
【发布时间】:2012-11-12 15:17:24
【问题描述】:

您好,我有一个 json 文件,其中包含以下数据并称为 textareasdata:

[
{"Select":"11","PhotoCount":"12"},
{"Select":"21","PhotoCount":"22"}
]

所以我想在我的 html 页面的 div 元素中显示第一个 Select 和 PhotoCount,我正在使用以下函数:

function fncLoadData(){

        $.getJSON('textareasdata.json', function(data) {
                        //alert(data);
            $("#txtTextArea").html(data);


        });
}

它在警告框上显示[object Object]。 请问我该怎么做,谢谢。

【问题讨论】:

    标签: jquery json


    【解决方案1】:
    function fncLoadData() {
        $.getJSON('textareasdata.json', function(data) {
            var firstSelect = data.shift();
            $("#txtTextArea").html(firstSelect.Select + " " + firstSelect.PhotoCount);
        });
    }
    

    【讨论】:

    • 谢谢大家,这行得通:function fncLoadData() { $.getJSON('textareasdata.json', function(data) { var firstSelect = data.shift(); $("#txtTextArea" ).html(firstSelect.Select + " " + firstSelect.PhotoCount); });但它让我只能访问 json 文件的第一行,现在,我该如何访问我的 json 文件中的第二行:这个 {"Select":"21","PhotoCount":"22" } 谢谢
    【解决方案2】:

    你有

    [ // <-- array of
    {"Select":"11","PhotoCount":"12"}, // <-- objects
    {"Select":"21","PhotoCount":"22"}
    ]
    

    所以要获取第一个对象,请使用 [index] - 数组索引为零

    data[0].Select  // get first select
    data[0].PhotoCount  // get first photocount
    

    【讨论】:

      【解决方案3】:
      $("#yourDIV").html(data[0].Select+data[0].PhotoCount);
      

      【讨论】:

        猜你喜欢
        • 2013-12-10
        • 1970-01-01
        • 2016-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-28
        相关资源
        最近更新 更多