javascript 如何在js中读取json对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14519041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:50:34  来源:igfitidea点击:

How to read a json object in js

javascriptjqueryjson

提问by Bhuvan

My controller has a method that is returning string representation of a jsonArray as

我的控制器有一个方法,它返回一个 jsonArray 的字符串表示为

jsonArray.toString()

jsonArray.toString()

Now following is the ajax method

现在下面是ajax方法

function loadPropertyFile(url) {
$.ajax({
    type: "GET",
    url: url, 
    dataType: "text",
    success: function(response){
        var obj = jQuery.parseJSON(response);
        alert(obj);
    }
});

}

}

Here the variable obj after parsing comes out to be

这里解析出来的变量obj是

"[{"portal.home":"Home"},{"displaytag.tracking.id":"Item ID"},{"displaytag.tracking.itemName":"Item Name"},{"displaytag.tracking.itemType":"Type"}]"

Now I want to access the values from the keys in js

现在我想从 js 中的键访问值

ie. I want to access the value of key "displaytag.tracking.id"

IE。我想访问键“displaytag.tracking.id”的值

Problem is when I am doing console.log(obj[0]["portal.home"]);It is giving me error TypeError: obj[0] is undefined

问题是当我在做console.log(obj[0]["portal.home"]); 它给了我错误TypeError: obj[0] is undefined

What shall I do ?

我该怎么办 ?

回答by VisioN

First you need to parse the JSON stringinto JavaScript object, and then access the required property:

首先需要将JSON 字符串解析为 JavaScript 对象,然后访问所需的属性:

var obj = JSON.parse(json);
console.log(obj[0]["portal.home"]);

In older browsers which do not have native JSON support, you should use something like Crockford's json2.js, which will give you one; please don't use eval() on JSON, as it can lead to pretty bad things all around.

在没有原生 JSON 支持的旧浏览器中,你应该使用 Crockford 的json2.js 之类的东西,它会给你一个;请不要在 JSON 上使用 eval(),因为它可能会导致非常糟糕的事情。

回答by Matt Zeunert

Use $.parseJSON(or JSON.parse in modern browsers) to convert your string into a Javascript object:

使用$.parseJSON(或现代浏览器中的 JSON.parse)将您的字符串转换为 Javascript 对象:

var json = '[{"portal.home":"Home"},{"displaytag.tracking.id":"Item ID"},{"displaytag.tracking.itemName":"Item Name"},{"displaytag.tracking.itemType":"Type"}]';
var object = $.parseJSON(json);

In your case your JSON string will create an array, so you will need to get the object at the correct index:

在您的情况下,您的 JSON 字符串将创建一个数组,因此您需要在正确的索引处获取对象:

var portalHomeValue = object[0]["portal.home"];

回答by h2ooooooo

In case you have the JSON directly in your javascript source (which I don't assume, but I'm adding this for others), you can just remove the quotes, and javascript creates an object based on it:

如果您的 javascript 源代码中直接包含 JSON(我不假设,但我为其他人添加了这个),您可以删除引号,javascript 会基于它创建一个对象:

var obj = [{"portal.home":"Home"},{"displaytag.tracking.id":"Item ID"},{"displaytag.tracking.itemName":"Item Name"},{"displaytag.tracking.itemType":"Type"}];
console.log(obj[0]["portal.home"]);

回答by Akshay Mishra

You can directly access by following

您可以通过以下方式直接访问

var data = JSON.parse('[{"Item_Number":"M71118LHB","Description":"MENS ONESIE"}]');
var desc = data[0].Description;
console.log(desc);