Javascript 如何获取 JSON 中的特定元素

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

How to get specific element in JSON

javascriptjqueryjson

提问by rabid_zombie

If I have JSON that is:

如果我有 JSON,那就是:

var response = {results: 2938; id: 9283};

How can I get id using javascript/jquery?

如何使用 javascript/jquery 获取 id?

I tried something like but I can't seem to get what I want (I want to get the number in id):

我试过类似的东西,但我似乎无法得到我想要的(我想得到 id 中的数字):

response[1]

and

response[1].id

采纳答案by Mark Coleman

Simple:

简单的:

response.id

With that being said, your json is invalid,

话虽如此,您的json无效,

var response = {results: 2938; id: 9283};

Use a ,to separate items not a ;

使用 a,来分隔项目而不是 a;

var response = {results: 2938, id: 9283};

And since I love jsfiddle so much, here is an example.

由于我非常喜欢 jsfiddle,这里有一个例子

回答by Quentin

Just response.id. You don't need anything more.

只是response.id。你不再需要任何东西。

回答by Jage

You should just need to use:

你应该只需要使用:

response.id

回答by integrater

dynamic func_param = JsonConvert.DeserializeObject(key);

foreach (var invoicekey in func_param)
{
    var invoice = merchelloHelper.Query.Invoice.GetByKey(Guid.Parse(invoicekey.Value));
}