0

The JSON file I have is following:

{
  "root": {
    "Quizsize": null,
    "{urn:abc.com/xmlns/mona/page}page": [
      {
        "@xid": "page1623",
        "@title": "Quiz Landing Page",
        "{urn:abc.com/xmlns/mona/page}comment": null,
        "{urn:abc.com/xmlns/mona/page}skills": null,
        "{urn:abc.com/xmlns/mona/page}info": {
          "{urn:abc.com/xmlns/mona/common}property": {
            "@name": "quiz_landing",
            "@value": "true"
          }
        }
      }
    ]
  }
}
}

I am loading this JSON file using :

var jsondata = eval("("+chapterRequest.responseText+")") ;
Root = jsondata.root

Now I want to access @xid which is "page1623 and @name which is "quiz_landing". I dont know how to do this,Please help.

Thanks

1
  • Could you clarify what you mean by 'access'? If you want to read property value, you have to loop through them, if you want a containing node, you have to loop through properties also and look for a matching value. Commented Jun 28, 2011 at 17:56

2 Answers 2

4

JSON.parse(x) is better than eval(x), for one. It is not supported natively by some browsers though. If you want to access @xid, "urn:abc.com/xmlns/mona/page}page" points to an array whose first element is an object.

So use Root["{urn:abc.com/xmlns/mona/page}page"][0]["@xid"]. Or mix bracket and dot notation. Your choice, really.

Sign up to request clarification or add additional context in comments.

3 Comments

Now the Error is: jsondata.root is undefined, its not able to access the root property.
Its working now,the problem was I was using some other JSON file that doesnot has root property..Thanks Steve
@Steve Wang: THX for ur answer "JSON.parse(x) is better than eval(x)"
1

When the key isn't a valid identifier, you use object['key'] instead of object.key, so jsondata.root['{urn:abc.com/xmlns/mona/page}page'][0]['@xid'].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.