0

When I do console.log(msg);. My response is this [{"test":"aaaaa"}].

Now, How do I select aaaa.

7
  • Try: console.log(d.test); From where you get response? Can you place more code? Commented Mar 25, 2013 at 6:55
  • 1
    Not a very clear question... Give more code. What do you mean by 'How do I select aaaa'? Commented Mar 25, 2013 at 6:55
  • You want to retrieve that data somehow ? Commented Mar 25, 2013 at 6:56
  • @freshbm:msg.test says undefined. obj = jQuery.parseJSON(msg); console.log(msg.test); also says undefined. Commented Mar 25, 2013 at 6:57
  • @mrfishie:Let me know, what is not clear to you ? Commented Mar 25, 2013 at 6:58

6 Answers 6

4

msg is an array, and the first element is {"test": "aaaaa"}

So you could do msg[0].test or msg[0]['test']

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

1 Comment

:It is not msg=[{"test":"aaaaa"}]. It is just [{"test":"aaaaa"}];
1

From Is there any key/value pair structure in JavaScript?

var myobj = {
  "managaner": ["Prateek","Rudresh","Prashant"],
  "employee": ["namit","amit","sushil"],
  "hr": ["priya","seema","nakul"]
}

alert(myobj['employee'][1]); // Outputs "amit"

Comments

1

try this

msg[0].test

or

msg[0]["test"]

1 Comment

:It is not msg=[{"test":"aaaaa"}]. It is just [{"test":"aaaaa"}];
1

Try this:

 $.each(msg, function(i, item){
    console.log(item.test);
 });

If you are getting your json response from some server or webservice then use it this way in your ajax success function(){}.

What going on with this is you have a json response as in array, so here $.each() is looping through the response it got from server then getting in the function with param item which is looping through the object. so at last console.log(item.test) is printing the object's property.

Comments

0

Example:

function(data) {
    var result = data[0];
    var was = result.was;
}

Comments

0

I understand that msg=[{"test":"aaaaa"}]

then u will have to take the value as msg[0].test . Then u can get "aaaaa"

2 Comments

:It is not msg=[{"test":"aaaaa"}]. It is just [{"test":"aaaaa"}];
watever it is .. from where you are getting [{"test":"aaaaa"}] . From the same variable, try to get the value