0

I am new to json and ajax this is my first example can any one help me out with this.

$.ajax({
    type: "GET",
     url: "ajs/index",
     dataType: "JSON",
     success: function(data) {          
     var obj = JSON.parse(data);
     $("#result").html(obj.name);
      }
   });

The output of data is of the form:

[Object {id=10, name="ss", title="ss", content="h", ...}, Object {id=12, name="lo", title="gi", content="c", ...}, Object {id=13, name="lo", title="gi", content="c", ...}, Object {id=14, name="lo", title="gi", content="c", ...}, Object {id=15, name="n", title="m", content="m", ...}]

The output of obj(after parsing) is of the form:

[{"id":10,"name":"ss","title":"ss","content":"h","created_at":"2014-07-07T10:07:02.398Z","updated_at":"2014-07-07T10:07:02.398Z"}]{"id":12,"name":"lo","title":"gi","content":"c","created_at":"2014-07-08T05:26:05.816Z","updated_at":"2014-07-08T05:26:05.816Z"}

when i use obj.name it is nt displaying any data how can i display all my data.

user2083041
  • 513
  • 1
  • 8
  • 32
  • I don't know which output you get at which point, but `data` is clearly an array, so you have to access one of its elements and get its name. The array itself doesn't have a name. – Felix Kling Jul 09 '14 at 04:07
  • possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Felix Kling Jul 09 '14 at 04:07
  • The output i mentioned for data : i got that output when i use console.log(data). After parsing it i got second output – user2083041 Jul 09 '14 at 04:10
  • Yeah, but the first output shows that `data` is already an array of objects (so no need to parse it). And second output implies that `obj` is a string which I find very hard to believe if the result comes from `JSON.parse`. It should throw an error if `data` is already an array. – Felix Kling Jul 09 '14 at 04:12
  • It doesn't throw any error. And after parsing i got output in "[ ]" square braces. I think we should not have [] – user2083041 Jul 09 '14 at 04:20

3 Answers3

1

The leading [ in your data indicates an array:

[{"id":10,"name":"ss","title":"ss","content":"h","created_at":"2014-07-07T10:07:02.398Z","updated_at":"2014-07-07T10:07:02.398Z"},{"id":12,"name":"lo","title":"gi","content":"c","created_at":"2014-07-08T05:26:05.816Z","updated_at":"2014-07-08T05:26:05.816Z"}]

In other words, obj is an array containing multiple objects. Each of those objects has parameters id, name, title, and so on.

So, you want to access obj[0].name, obj[1].name, etc., not just obj.name.

Note: There was a typo in the post-parsing data that you provided in the question. It looks like it should be what I have above. If you still have problems, please let me know in the comments.

elixenide
  • 44,308
  • 16
  • 74
  • 100
  • Thanks for your valuable information. I have changed my succes function as success: function(data) { var obj = JSON.parse(data); alert(obj[0].name); But it is not showing alert box – user2083041 Jul 09 '14 at 05:01
  • I process worked for data output not for obj that is data[0].name got worked – user2083041 Jul 09 '14 at 05:07
  • one small think i want to say is json means javascript object notation the output i got in data is of object format so i think there is no need of parsing it. Am i thinking it in correct format – user2083041 Jul 09 '14 at 05:10
  • It's hard to tell from your question; if you got the output shown for the object, then yes, you are getting back a JSON object with no need to parse it. If you are able to parse it and getting the data specified, then that indicates that you actually got a string. How did you produce the values in your question? – elixenide Jul 09 '14 at 05:15
  • just by using data[0].name i got output. My data contains object then after parsing it i got output of the format [{"id":1,"name":"jhdb"},{"id":1,"name":"jhdb"},{"id":1,"name":"jhdb"}] then what is the above format is called after parsing – user2083041 Jul 09 '14 at 05:29
  • Try logging `data` to the console in Chrome (`console.log(data)`), then try logging the parsed version (`console.log(JSON.parse(data))`). Something is very wrong with your AJAX response if you are able to parse `data` but my solution doesn't work for you. – elixenide Jul 09 '14 at 05:32
  • what is the difference between two outputs i specified both are called as array of objects but the format is different why? any answer – user2083041 Jul 09 '14 at 06:03
  • I can't really answer this because I can't tell what output you're actually getting. The presence of the word `Object` in the first version makes it look like your browser is trying to convert an object, but then you post the converted version, which looks like a JSON object. One of these is incorrect, but I have no way of knowing which one. Either use `data` without calling `JSON.parse`, or call `JSON.parse` and use the result -- I can't tell which you need based on the information given. Either way, you need to use the data as an array, as shown in my answer. – elixenide Jul 09 '14 at 06:05
0

I would use the convenience method

$.getJSON('/someurl',function(jsonReturnedFromServer){ });

You may have to debug it to ensure you're getting valid json back from the server

TGH
  • 38,769
  • 12
  • 102
  • 135
0

One thing you can do is use the Chrome developer tools to set a breakpoint in your code at the point in which you get a response and set it to a variable. At this point Chrome has a console you can use to view objects and execute javascript. It is incredibly handy for situations like this.

It looks like the data is coming back as a list, so you would want to do something like this.

for(var i = 0; i < data.length; i++) {
    var name = data[i].name;
    // you can manipulate the DOM here with the name
}
user2108599
  • 254
  • 1
  • 3
  • i got response for data in object format which i mentioned in my question after parsing i got another format now i have to display that data – user2083041 Jul 09 '14 at 04:07
  • Yea, if you just play around with the console you could see what happens when you do obj.name. Im not a javascript expert, but you might have to do obj['name']. Ive had that issue in the past – user2108599 Jul 09 '14 at 04:11
  • It is not displaying any thing if you observe the second output that is after parsing I am getting output in "[ ]" square braces. but i think we should not have that braces – user2083041 Jul 09 '14 at 04:18
  • You should probably check the value of the variable `data` before you parse it to ensure that a value exists. If it does, I would use breakpoints and manually check all the response before and after it gets parsed to see where it is going wrong – user2108599 Jul 09 '14 at 04:28
  • But does it have the expected contents? – user2108599 Jul 09 '14 at 04:36
  • The output after parsing is of the format [{"id":1,"name":"sdh"},{"id":1,"name":"sdh"},{"id":1,"name":"sdh"}]. now i need to display name from this output how can i the data is enclosed in between square braces – user2083041 Jul 09 '14 at 04:49
  • So your response is coming back as a list. You're gonna have to iterate over it to grab what you need – user2108599 Jul 09 '14 at 04:58
  • I have solved it before in the same manner but thanks for your suggestion. – user2083041 Jul 09 '14 at 05:08