I have an xml file that has been converted to the json listed below. I have been trying to figure out how to retrieve -Name and -Value from each of the Attributes with no luck. 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. 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
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.
To solve your problem you can access the properties using other way.
Otherwise your code: jsonobj.Media.Attribute[i].-Name
You can use: jsonobj.Media.Attribute[i].["-Name"]
What is the same from calling, for an example: jsonobj.["Media"].Attribute[i].["-Name"]
It can not identify key Attribute. Says can not read property 'Attribute' of undefined.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.