1

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"
              }
            ]
          }
        };
4
  • 1
    Hint: You can use ['-Name'] to access elements of the object. Commented Feb 27, 2013 at 19:25
  • iterate jsonobj.Media.Attribute and use ['-Name'] to retrieve the value. Commented Feb 27, 2013 at 19:28
  • I've create a subobject and attempted to access it using subobject[0].Name (returns undefined). I've tried subobject[0].-Name (throws an error), If I just do a console.debug on subobj, I get Object {-Name: "Keywords", -Value: "keyword value"}. I can't seem to get anything else from it. Commented Feb 27, 2013 at 19:29
  • Definitely, you cannot use subobject[0].-Name to access the object because - is an operator. If you execute the code, you get an error. Change it to subobject[0].["-Name"] will fix the problem. Commented Feb 27, 2013 at 19:31

4 Answers 4

2

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent! Thank you. Very clear what is going on once I see the code.
1

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"]);
}

Comments

0

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"]

Comments

0

It can not identify key Attribute. Says can not read property 'Attribute' of undefined.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.