最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: Parse JSON output result - Stack Overflow

programmeradmin2浏览0评论

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: ".php?v=json&i=[[102]]",
onComplete: function (response) {
var data = response.json[0];
console.log("User: " + data.username); // doesnt work

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./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 edited Jul 19, 2013 at 4:41 Natsume asked Jul 19, 2013 at 3:38 NatsumeNatsume 9013 gold badges15 silver badges22 bronze badges 5
  • Can you post what you tried? – Alexander Puchkov 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. – Mics Commented Jul 19, 2013 at 3:43
  • @AlexPuchkov updated codes that i tried – Natsume Commented Jul 19, 2013 at 3:45
  • Make sure you provided "json" dataType in $.ajax call – Alexander Puchkov 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 – aaronman Commented Jul 19, 2013 at 3:50
Add a ment  | 

3 Answers 3

Reset to default 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.

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

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);
发布评论

评论列表(0)

  1. 暂无评论