Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How can i retrieve the values from such JSON response with javascript, I tried normal JSON parsing seem doesn't work

[["102",true,{"username":"someone"}]]

Tried such codes below:

url: "http://somewebsite.com/api.php?v=json&i=[[102]]",
onComplete: function (response) {
var data = response.json[0];
console.log("User: " + data.username); // doesnt work
share|improve this question
    
Can you post what you tried? –  Alexander Puchkov Jul 19 '13 at 3:40
1  
No quotes around someone; That's not JSON response. It's JYON: json-like your own notation. –  Mics Jul 19 '13 at 3:43
    
@AlexPuchkov updated codes that i tried –  Natsume Jul 19 '13 at 3:45
    
Make sure you provided "json" dataType in $.ajax call –  Alexander Puchkov Jul 19 '13 at 3:46
    
@Natsume can you please explain more clearly your problem is, because my answer explains what you had originally, but I'm not sure if it's really what you want –  aaronman Jul 19 '13 at 3:50

3 Answers 3

up vote 3 down vote accepted

So the problem with this is that it looks like an array in an array. So to access an element you would do something like this.

console.log(obj[0][0]);  

should print 102

Lets say you created the object like so:

var obj = [["102",true,{"username":someone}]];  

this is how you would access each element:

obj[0][0] is 102

obj[0][1] is true
and obj[0][2]["username"] is whatever someone is defined as

From other peoples answers it seems like some of the problem you may be having is parsing a JSON string. The standard way to do that is use JSON.parse, keep in mind this is only needed if the data is a string. This is how it should be done.

var obj = JSON.parse(" [ [ "102", true, { "username" : someone } ] ] ")
share|improve this answer
    
You answer works! But however i can't use JSON.parse since its due to Firefox's SDK, so i done dt = obj.json[0][2], thought when i did dt.username the console will say "undefined", but i'm sure there is an error during response request URL, i'm sure i can manage the URL error part. +1 for you :D –  Natsume Jul 19 '13 at 4:39
    
@Natsume in your original post it looked like someone was a variable, it must be defined for the JSON to work –  aaronman Jul 19 '13 at 4:41
    
oops that was suppose to be string, i forgot to include "" D:. Fixed it :P –  Natsume Jul 19 '13 at 4:42
var str = '[["102",true,{"username":"someone"}]]';
var data = JSON.parse(str);
console.log("User: " + data[0][2].username);
  1. Surround someone with double quotes
  2. Traverse the array-of-array before attempting to acces the username property

If you are using AJAX to obtain the data, @Alex Puchkov's answer says it best.

share|improve this answer
    
I edited my answer to include JSON.parse how did you infer that is indeed what the OP wants –  aaronman Jul 19 '13 at 4:31
    
@aaronman well if you're turning a string into an object in JS, that's the only real way to do it. Most other methods (including jQuery's $.ajax) use JSON.parse themselves. In this particular case, the OP had not provided enough information, but an educated guess was that @ @Natsume was indeed using $.ajax, and also that most likely either the parameters for XHR were missing something, or the PHP server was returning the wrong content type, and the returned "object" was actually a string. H/W the question was pointedly about parsing, and not about XHR, hence my answer. –  bguiz Jul 21 '13 at 23:45
    
little late, apparently JSON.parse was not what he wanted, honestly I'm still not sure what he wanted I UV'd u anyway –  aaronman Jul 21 '13 at 23:48
    
@aaronman yep, same here! –  bguiz Jul 21 '13 at 23:57

It depends on where you are getting JSON from:

If you use jQuery

then jQuery will parse JSON itself and send you a JavaScript variable to callback function. Make sure you provide correct dataType in $.ajax call or use helper method like $.getJSON()

If you getting JSON data via plain AJAX

then you can do:

var jsonVar = JSON.parse(xhReq.responseText);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.