简体   繁体   English

Javascript:从 JSON 对象数组访问值

[英]Javascript: Accessing the values from JSON Object Array

I receive a JSON string returned from a remote Webservice.我收到从远程 Web 服务返回的 JSON 字符串。 Something like this:像这样的东西:

[{
    "id": "001",
    "link": "<a href=\"https://www.google.com\">Google</a>"
}, {
    "id": "002",
    "link": "<a href=\"https://www.yahoo.com\">Yahoo!</a>"
}]

But then when i try to parse it in my Javascript, i have problem parsing it.但是当我尝试在我的 Javascript 中解析它时,我在解析它时遇到了问题。 I tried:我试过:

var response_string = '[{"id":"001","link":"<a href=\"https://www.google.com\">Google</a>"},{"id":"002","link":"<a href=\"https://www.yahoo.com\">Yahoo!</a>"}]';
console.log("Output (1) --> ", response_string);

response_string = JSON.stringify(response_string);
console.log("Output (2) --> ", response_string);

var response_object = JSON.parse(response_string);
console.log("Output (3) --> ", response_object);

console.log("Value: " + response_object[0].id);
console.log("Value: " + response_object[0]['id']);

Then the outputs are like:然后输出如下:

Output (1) -->  [{"id":"001","link":"<a href="https://www.google.com">Google</a>"},{"id":"002","link":"<a href="https://www.yahoo.com">Yahoo!</a>"}]

Output (2) -->  "[{\"id\":\"001\",\"link\":\"<a href=\"https://www.google.com\">Google</a>\"},{\"id\":\"002\",\"link\":\"<a href=\"https://www.yahoo.com\">Yahoo!</a>\"}]"

Output (3) -->  [{"id":"001","link":"<a href="https://www.google.com">Google</a>"},{"id":"002","link":"<a href="https://www.yahoo.com">Yahoo!</a>"}]

Value: undefined

Value: undefined

How do i access the values inside this object please?请问如何访问该对象内的值?

You cannot simply copy-paste the raw response as-is and use it as a string literal, since the string literals have their own syntactic rules.您不能简单地按原样复制粘贴原始响应并将其用作字符串文字,因为字符串文字有自己的语法规则。

One of those - is using escape sequences, is where you've made a mistake.其中之一 - 使用转义序列,就是您犯了错误的地方。

If you want to use it in place you must escape every backslash manually, like如果你想就地使用它,你必须手动转义每个反斜杠,比如

 var response_object = JSON.parse('[{"id": "001","link": "<a href=\\\\"https://www.google.com\\\\">Google</a>"}, {"id": "002","link": "<a href=\\\\"https://www.yahoo.com\\\\">Yahoo!</a>"}]'); // --------------------------------------------------------------^^-----------------------^^----------------------------------------------^^----------------------^^ console.log("Value: " + response_object[0].id);

For the code that retrieves the JSON string from a remote server you don't need to make any alterations and simply JSON.parse() would parse it just fine.对于从远程服务器检索 JSON 字符串的代码,您不需要进行任何更改,只需JSON.parse()就可以很好地解析它。

So assuming response_string contains the whole response from a remote server with a mentioned JSON string (without any additional processing), then this:因此,假设response_string包含来自远程服务器的整个响应以及一个提到的 JSON 字符串(没有任何额外的处理),那么这个:

var response_object = JSON.parse(response_string);
console.log("Output (3) --> ", response_object);

code would work.代码会起作用。

The problem is that the webservice sends invalid JSON.问题是,web服务发送无效JSON。

{"JSON": "This is "not" valid JSON" }

The quotation marks in any JSON have to be escaped :任何 JSON 中的引号都必须转义

{"JSON": "This is \"valid\" JSON" }

Any Unicode character except " or \\ will be treated by parser as it was.除了"\\之外的任何 Unicode 字符都将被解析器按原样处理。

The " represents termination, while \\ is treated differently depending on which character comes after it. For example \\n , here the letter n comes after \\ it will be treated as newline character. Below picture taken from json.org describes it better: "表示终止,而\\根据后面的字符而不同。例如\\n ,这里的字母n\\之后将被视为换行符。下面从json.org截取的图片对其进行了更好的描述:

So, you should report this bug to the webservice providers.因此,您应该将此错误报告给 Web 服务提供商。 It has to be fixed.它必须被修复。

Parse json like this像这样解析json

var res = JSON.parse('[{"id": "001","link": "<a href=\\"https://www.google.com\\">Google</a>"}, {"id": "002","link": "<a href=\\"https://www.yahoo.com\\">Yahoo!</a>"}]');
console.log(res);
console.log(res[0].id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM