在JavaScript中读取一个简单的JSON文件

时间:2015-04-10 04:39:10

标签: javascript html json web

我有一个JSON文件,其中包含以下内容:

{
    "residents": [
        {
            "name" : "Jacob",
            "title" : "King",
            "gender" : "Male",
        },
        {
            "name" : "Luthor",
            "title" : "Prince",
            "gender" : "Male"
        },
        {
            "name" : "Mileena",
            "title" : "Princess",
            "gender" : "Female"
        },
    ]
}

我想在JavaScript中阅读json,但当然,我不知道。我怎么会遇到这个问题?

10 个答案:

答案 0 :(得分:0)

取决于您使用的环境 如果您使用node.js,则应使用API​​ - fileRead来阅读此文件 然后你应该使用JSON.parse在{Object}

中解析它

如果您在浏览器中工作且某个服务器具有此文件的静态路径,您可以使用ajax获取此文件

在这两种情况下,您都应该执行以下步骤:

  1. 以{String}
  2. 获取文件
  3. 解析为{Object}

答案 1 :(得分:-1)

试试这个..

var mymessage='{
    "residents": [
        {
            "name" : "Jacob",
            "title" : "King",
            "gender" : "Male",
        },
        {
            "name" : "Luthor",
            "title" : "Prince",
            "gender" : "Male"
        },
        {
            "name" : "Mileena",
            "title" : "Princess",
            "gender" : "Female"
        },
    ]
}';
var jsonData = JSON.parse(myMessage);
for (var i = 0; i < jsonData.residents.length; i++) {
    var resident= jsonData.residents[i];
    console.log(resident.name);
}

答案 2 :(得分:-1)

这样的东西?

&#13;
&#13;
$(function () {

    $("#btnTest").click(function () {

        var data = {
            "residents": [{
                "name": "Jacob",
                    "title": "King",
                    "gender": "Male",
            }, {
                "name": "Luthor",
                    "title": "Prince",
                    "gender": "Male"
            }, {
                "name": "Mileena",
                    "title": "Princess",
                    "gender": "Female"
            }, ]
        };
     //If you're getting it somewhere from ajax call, than possibly it would be in string , in that case you need to `parse` it as  data = JSON.parse(data);
        $.each(data.residents, function (index, value) {
            $("#data").append(value.name + " " + value.title + " " + value.gender + " </br>");
        });

    });

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="btnTest" value="Try Me!" />
<div id="data"> 
  </div>
&#13;
&#13;
&#13;

答案 3 :(得分:-1)

假设这是因为您的网络代码而适用于网络,最简单的方法是使用jquery

$.get('http://ip.jsontest.com', function(data) { console.log(data); });

如果服务器在MIME的响应中使用了正确的JSON类型,则jquery会将其转换为对象,上例中的“data”将是已评估的JSON。

如果服务器没有使用正确的MIME类型,您可以将其包装在JSON.parse:

 var json = JSON.parse(data);

答案 4 :(得分:-1)

你可以使用JQuery。

$.ajax({
   url: "\data.json", //name of your json file
   success: function (data) {
      var obj = JSON.parse(data);
   }
});

然后你可以通过以下方式获得必要的财产:

obj.residents[0].name

等等。

答案 5 :(得分:-1)

使用javascript你就可以......

obj = JSON.parse({
"residents": [
    {
        "name" : "Jacob",
        "title" : "King",
        "gender" : "Male",
    },
    {
        "name" : "Luthor",
        "title" : "Prince",
        "gender" : "Male"
    },
    {
        "name" : "Mileena",
        "title" : "Princess",
        "gender" : "Female"
    },
]
});

您现在可以在可以管理的对象中使用JSON

console.log(obj);

答案 6 :(得分:-1)

你可以像下面那样获取它

var text = '{
"residents":[
{
    "name" : "Jacob",
    "title" : "King",
    "gender" : "Male",
},
{
    "name" : "Luthor",
    "title" : "Prince",
    "gender" : "Male"
},
{
    "name" : "Mileena",
    "title" : "Princess",
    "gender" : "Female"
},
]

}'; // That is your data from request

var obj = JSON.parse(text);
alert(obj.residents);
alert(obj.residents[0].name);

答案 7 :(得分:-1)

<script>
    json_text = '{
    "residents":[
             {
                 "name" : "Jacob",
                 "title" : "King",
                 "gender" : "Male",
             },
             {
                 "name" : "Luthor",
                 "title" : "Prince",
                 "gender" : "Male"
             },
             {
                 "name" : "Mileena",
                 "title" : "Princess",
                 "gender" : "Female"
             },
        ]}';
        var obj = JSON.parse(json_text);
        alert(obj.residents[2].name);
</script>

此代码将在浏览器中显示一个警告对话框,其中包含数组name中第二个对象的residents属性值。

答案 8 :(得分:-1)

首先,你的json字符串有错误。

{
"residents": [
    {
        "name" : "Jacob",
        "title" : "King",
        "gender" : "Male",
    },
    {
        "name" : "Luthor",
        "title" : "Prince",
        "gender" : "Male"
    },
    {
        "name" : "Mileena",
        "title" : "Princess",
        "gender" : "Female"
    },
]

}

从最后的第三个括号后面不会有逗号。

JSONString = '{ . . . . }'; JSONObject = JSON.parse(JSONString);

这样您就可以从JSONObject访问json数据。

答案 9 :(得分:-1)

试试这个

<!docTpye html>
<html>
<head>
<script>
var data={
    "residents": [
        {
            "name" : "Jacob",
            "title" : "King",
            "gender" : "Male",
        },
        {
            "name" : "Luthor",
            "title" : "Prince",
            "gender" : "Male"
        },
        {
            "name" : "Mileena",
            "title" : "Princess",
            "gender" : "Female"
        },
    ]
}
for(var i=0;i<data.residents.length;i++){
console.log(data.residents[i].name);
alert(data.residents[i].name);
}
</script>
</head>
<body>
</body>
</html>