2

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
5
  • Can you post what you tried? Commented Jul 19, 2013 at 3:40
  • 1
    No quotes around someone; That's not JSON response. It's JYON: json-like your own notation. Commented Jul 19, 2013 at 3:43
  • @AlexPuchkov updated codes that i tried Commented Jul 19, 2013 at 3:45
  • Make sure you provided "json" dataType in $.ajax call Commented Jul 19, 2013 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 Commented Jul 19, 2013 at 3:50

3 Answers 3

4
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.

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

3 Comments

I edited my answer to include JSON.parse how did you infer that is indeed what the OP wants
@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.
little late, apparently JSON.parse was not what he wanted, honestly I'm still not sure what he wanted I UV'd u anyway
3

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

3 Comments

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 in your original post it looked like someone was a variable, it must be defined for the JSON to work
oops that was suppose to be string, i forgot to include "" D:. Fixed it :P
3

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);

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.