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] => []
)
3
  • Can you post what you have tried to do? Commented Jun 2, 2014 at 8:07
  • stackoverflow.com/questions/2295496/convert-array-to-json Commented Jun 2, 2014 at 8:08
  • what produced that? The keys are not a legal syntax for a JS object. Commented Jun 2, 2014 at 8:12

3 Answers 3

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?

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

Comments

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.

2 Comments

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

6 Comments

var data = JSON.parse(JSON.stringify(yourArray));
No problem, just finished it myself and didn't want to create a new answer.
"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 if my post was helpful, don't be a shame to vote up the answere :D
I think he did, but I've downvoted because it completely missed the point - the data supplied wasn't JSON at all.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.