[英]Retrieving values from JSON object
I have an xml file that has been converted to the json listed below. 我有一个已转换为下面列出的json的xml文件。 I have been trying to figure out how to retrieve -Name and -Value from each of the Attributes with no luck.
我一直试图弄清楚如何从每个属性中检索-Name和-Value而没有运气。 I'm guessing I need to create a sub-object that is equal to jsonobj.Media.Attribute[i], but am unable to access -Name or -Value once I do that.
我猜我需要创建一个等于jsonobj.Media.Attribute [i]的子对象,但是一旦我这样做就无法访问-Name或-Value。 Any suggestions?
有什么建议么?
jsonobj= {
"Media": {
"Attribute": [
{
"-Name": "Keywords",
"-Value": "keyword value"
},
{
"-Name": "Title",
"-Value": "title value"
},
{
"-Name": "Description",
"-Value": "description value"
},
{
"-Name": "Author",
"-Value": "author value"
},
{
"-Name": "Copyright",
"-Value": "copyright value"
}
]
}
};
This will alert all the values you're looking for: 这将提醒您正在寻找的所有值:
var list = jsonobj.Media.Attribute
for(index in list)
{
var obj = list[index];
var name = obj["-Name"];
var value = obj["-Value"];
alert(name);
alert(value);
}
Iterate jsonobj.Media.Attribute and use ['-Name'] to retrieve the value 迭代jsonobj.Media.Attribute并使用['-Name']检索值
for(var i = 0; i < jsonobj.Media.Attribute.length ; i++)
{
var attr = jsonobj.Media.Attribute[i]
alert(attr["-Name"]);
alert(attr["-Value"]);
}
You can not use -
in the code, cause it is an operator, and JS will not recognize it as a method. 你不能在代码中使用
-
因为它是一个操作符,JS不会将它识别为方法。
To solve your problem you can access the properties using other way. 要解决您的问题,您可以使用其他方式访问属性。
Otherwise your code: jsonobj.Media.Attribute[i].-Name
否则你的代码:
jsonobj.Media.Attribute[i].-Name
You can use: jsonobj.Media.Attribute[i].["-Name"]
您可以使用:
jsonobj.Media.Attribute[i].["-Name"]
What is the same from calling, for an example: jsonobj.["Media"].Attribute[i].["-Name"]
例如,调用有什么相同:
jsonobj.["Media"].Attribute[i].["-Name"]
It can not identify key Attribute. 它无法识别关键属性。 Says can not read property 'Attribute' of undefined.
说不能读取未定义的属性'属性'。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.