javascript 无法访问 JSON 对象属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7227764/
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
Can't access JSON object properties
提问by Tsundoku
I am receiving back from the server a JSON string like this one:
我从服务器收到一个像这样的 JSON 字符串:
[{"Title":"Windows","URL":"http:\/\/www.domain.com\/soft\/","Type":"out","Price":"140"}]
I save it into a variable string
and I am trying to convert it to a JSON object like this:
我将它保存到一个变量中,string
并尝试将其转换为这样的 JSON 对象:
var json = JSON.parse(string);
after that I get the Object which looks great:
之后我得到了看起来很棒的对象:
[Object]
->Price: "140"
->Title: "Windows"
->Type: "out"
->URL: "http:www.domain.com/soft/"
->__proto__: Object
but when I try to acces it using for example json.Price
I get undefined
, any idea what I'm missing here?
但是当我尝试使用例如json.Price
我得到它时undefined
,知道我在这里遗漏了什么吗?
回答by Andreas K?berle
As you wrap your content with [] you get an array with one object. So this should work:
当你用 [] 包裹你的内容时,你会得到一个包含一个对象的数组。所以这应该有效:
json[0].Price
But you can also remove the brackets.
但您也可以删除括号。