-4

Possible Duplicate:
Reading JSON string in Jquery

The following Json String

{
   "direct":{
      "newvisits":3,
      "uniquevisits":3,
      "retvisits":0
   },
   "user":{
      "newvisits":4,
      "uniquevisits":4,
      "retvisits":0
   }
}

The elements "direct" and "user" etc. are dynamically filled values.

How can I access the values of "direct" and "user" using jquery? In detail Want to fetch the name of "direct" and "user"[dyanamically filled fields] and then the values under each category

3
  • 1
    Possible duplicates: stackoverflow.com/questions/2524489/… and stackoverflow.com/questions/3005606/jquery-json-parsing Commented Jul 25, 2012 at 6:32
  • 1
    Take a look at $.parseJSON()... You'll have to convert that string into a JSON object before you can access the properties. Is this data coming from an AJAX call perhaps? Commented Jul 25, 2012 at 6:34
  • Ya..me encoded a string indexed multi dimensional array and encode json using php and return the value to jquery Commented Jul 25, 2012 at 6:40

1 Answer 1

1

Assuming retVal is the the JSON object.

retVal.direct or retVal["direct"] will give you direct object.

EDIT For getting the keys you can do.

for(key in retVal){
   retVal['key'] // something like this.
}
Sign up to request clarification or add additional context in comments.

2 Comments

I am sorry..actuall i doesnot mean how to fetch the value of direct or user,First I wanna fetch the name "direct",then the values in side direct
"direct" would be in key there and its values in retVal['key']

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.