0

I not which part I am doing wrong. I couldn't able to fetch this array to display. Can someone please help me with this. I am new to JSON.

Array
(
    [0] => [{"id":2,"request_id":2,"message":"wqvewq ewq wq ewq e wqwe  qwe ","user_id":1,"created_at":"2014-05-30 16:21:28","updated_at":"2014-05-30 16:21:28"},{"id":3,"request_id":2,"message":"as aS A","user_id":2,"created_at":"2014-05-30 17:18:37","updated_at":"2014-05-30 17:18:37"},{"id":4,"request_id":2,"message":"AS As a","user_id":2,"created_at":"2014-05-30 17:18:43","updated_at":"2014-05-30 17:18:43"}]
    [1] => [{"id":1,"request_id":2,"message":"sfsdfds sdfds f   ","user_id":2,"created_at":"2014-05-30 17:15:16","updated_at":"2014-05-30 17:15:16"}]
    [2] => []
)
Alnitak
  • 334,560
  • 70
  • 407
  • 495
winnyboy5
  • 1,486
  • 3
  • 22
  • 45

3 Answers3

1

The output you have quoted looks like PHP print_r output, and it's certainly not legal JSON.

Perhaps you need the PHP json_encode function, to get real JSON out of your PHP code?

Alnitak
  • 334,560
  • 70
  • 407
  • 495
0

It's not one json string but an array of json strings. You have to first loop thru the array, parse the json and show the variables that you want in your html with jQuery.

You can find a lot of info on the internet and stackoverflow on this subject.

Mathias Dewelde
  • 665
  • 9
  • 19
  • 2
    no, it is _not_ an array of JSON strings. This is (mostly) a plain and simple object literal. The keys aren't in the right syntax, though... – Alnitak Jun 02 '14 at 08:11
  • 1
    You probably should have tagged your post with `laravel` and `php`, then... I expect you'll be wanting the PHP `json_encode` function to handle sending this structure over HTTP to the jQuery client, which if you're using normal jQuery code will automatically convert it back into an object structure. – Alnitak Jun 02 '14 at 08:20
0

these are the possibilitys you have

var data = array();
for(var i=0;i<yourArray.length;i++)
    data[i] = $.parseJSON(yourArray[i]);

or (untested)

var data = JSON.parse(JSON.stringify({yourArray: yourArray}));
Dwza
  • 6,494
  • 6
  • 41
  • 73
  • var data = JSON.parse(JSON.stringify(yourArray)); – Mathias Dewelde Jun 02 '14 at 08:10
  • No problem, just finished it myself and didn't want to create a new answer. – Mathias Dewelde Jun 02 '14 at 08:12
  • "JSON.parse: unexpected character at line 1 column 1 of the JSON data". This error throws off. Probably the array function at the start – winnyboy5 Jun 02 '14 at 08:17
  • @winnyboy5 if my post was helpful, don't be a shame to vote up the answere :D – Dwza Jun 02 '14 at 09:23
  • I think he did, but I've downvoted because it completely missed the point - the data supplied wasn't JSON at all. – Alnitak Jun 02 '14 at 09:25
  • @Alnitak but as you can see, it WAS helpful for him and the quation wasn't `where is my error...` according to his question `How to display this json in jquery` my answere IS right! echoing and working with the result could help him as well – Dwza Jun 02 '14 at 10:08