[英]How to access element in JSON and Javascript
How do I access something like this with Javascript and Jquery? 如何使用Javascript和Jquery访问类似内容?
"condition" : [{"conditionId":["3000"],"conditionDisplayName":["Used"]}]
(this is just an excerpt) (这只是摘录)
I tried using item.condition and I got: 我尝试使用item.condition并得到:
[{"conditionId":["3000"],"conditionDisplayName":["Used"]}]
as a result. [{"conditionId":["3000"],"conditionDisplayName":["Used"]}]
。
How do I get the conditionDisplayName
? 我如何获得
conditionDisplayName
?
I've also tried using : item.condition.conditionDisplayName
but it doesn't work nor does item.condition[1]
我也尝试过使用:
item.condition.conditionDisplayName
但它也不起作用, item.condition[1]
也不起作用
You're almost there but your condition
is an array with one object so you need to use the array notation to access it: 您快到了,但是您的
condition
是一个带有一个对象的数组,因此您需要使用数组符号来访问它:
var displayName = item.condition[0].conditionDisplayName;
If there could be more than one object in the array, you can use a loop: 如果数组中可以有多个对象,则可以使用循环:
for(var i=0; i<item.condition.length; i++) {
console.log( item.condition[i].conditionDisplayName );
}
condition
is an array, as is conditionDisplayName
, so you will need to access their members using an index. condition
是一个数组,就像conditionDisplayName
,因此您将需要使用索引访问它们的成员。 For example, to get the first displayName of the first condition you would do: 例如,要获取第一个条件的第一个displayName,您将执行以下操作:
var displayName = item.condition[0].conditionDisplayName[0]
您可以使用数组索引获取对象
var displayName = obj.condition[0].conditionDisplayName;
在您的情况下,您应该尝试:
item.condition[0].conditionDisplayName
var array = [{"conditionId":["3000"],"conditionDisplayName":["Used"]}]
// Not right - show ["Used"]
console.log(array[0].conditionDisplayName);
// Right - show "Used"
console.log(array[0].conditionDisplayName[0]);
You can see this here: http://jsfiddle.net/bs9kx/ 您可以在这里看到: http : //jsfiddle.net/bs9kx/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.