在 Javascript 中访问 Json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7893494/
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 01:40:12  来源:igfitidea点击:

Accessing Json in Javascript

javascriptjson

提问by slandau

'[{"SponsorID":382,"SponsorName":"Test Name","MonthEndReport":true,"AccountingManager":"Me","UnboundProperties":[],"State":16}]'

When I try to access the above like this:

当我尝试像这样访问上述内容时:

for (var i = 0; i < data.length; i++) {
    alert(data[i]);
}

It spells out each thing, such as [, {, ", S, and etc.

它阐明了每件事,例如[, {, ", S,等等。

I also tried doing data[i].SponsorNamebut obviously got undefined. How should I be accessing this?

我也尝试过,data[i].SponsorName但显然得到了undefined. 我应该如何访问这个?

回答by JAAulde

You need to parse the JSON string, preferably with JSON.parse. The JSONAPI is built into more modern browsers and can be provided to older browsers by including Crockford's JSON script. Crockford's script will detect if the browser already provides the API and adds it if not.

您需要解析 JSON 字符串,最好使用JSON.parse. 该JSONAPI内置更现代的浏览器,可以通过包括提供给旧的浏览器克罗克福德的JSON脚本。Crockford 的脚本将检测浏览器是否已经提供了 API,如果没有,则添加它。

With that in place, if your JSON is in a string variable named response, you can:

有了这个,如果您的 JSON 位于名为 的字符串变量中response,您可以:

var parsedResponse = JSON.parse( response );
//run your iterating code on parsedResponse

回答by Michael Berkowski

You would first need to eval()or more ideally JSON.parse()the JSON string in to a Javascript object. This assumes you trust the source of the JSON.

您首先需要eval()或更理想地JSON.parse()将 JSON 字符串放入 Javascript 对象中。这假设您信任 JSON 的来源。

var jsonobj = JSON.parse(data); 
// Now view the object's structure
console.dir(jsonobj);

Here's what it looks like after being evaluated and printed out:

这是经过评估和打印后的样子:

Screen capture of the JSON obj

JSON 对象的屏幕截图

回答by Joe

var array = JSON.parse('[{"SponsorID":382,"SponsorName":"Test Name","MonthEndReport":true,"AccountingManager":"Me","UnboundProperties":[],"State":16}]')
array[0].AccountingManager; // "me"

Or everyone's favorite library, since IE7 and below don't have native support:

或者大家最喜欢的库,因为 IE7 及以下没有原生支持:

$.parseJSON('[{"SponsorID":382,"SponsorName":"Test Name","MonthEndReport":true,"AccountingManager":"Me","UnboundProperties":[],"State":16}]')

回答by Roman

You parsed the Json string first, right?

你先解析了 Json 字符串,对吧?

var data = '[{"SponsorID":382,"SponsorName":"Test Name","MonthEndReport":true,"AccountingManager":"Me","UnboundProperties":[],"State":16}]';
data = JSON.parse(data);
alert(data.SponsorName);

JSON.parse, when available, is the preferred method over "eval" because of security and performance issues.

由于安全和性能问题,JSON.parse(如果可用)是优于“eval”的首选方法。

回答by smoak

You've got a JSON array followed by an object:

您有一个 JSON 数组,后跟一个对象:

var data = [{"SponsorID":382,"SponsorName":"Test Name","MonthEndReport":true,"AccountingManager":"Me","UnboundProperties":[],"State":16}];

alert(data[0].SponsorID);