2

I have this on my console on firebug,

[Object { fa_id="1167535",  f_id="1000",  loc_type="6",  more...}, Object { fa_id="1167535",  f_id="1000",  loc_type="6",  more...}]

it is data from the server side. Now, how would I convert this into array such that this data can be used on another file. I tried JSON.parse and jQuery.parseJSON but both did not work.

7
  • 3
    Your tags slightly confuse the matter. Do you want to get it into an array in JavaScript, or PHP? Commented Feb 21, 2013 at 18:44
  • again confusing .. JavaScript or php ? Commented Feb 21, 2013 at 18:48
  • You don't have JSON. JSON is a string representation of data, that just so happens to share syntax with JavaScript. You have an array of objects, what are you trying to do with it? Commented Feb 21, 2013 at 18:48
  • 1
    So why did you try $.parseJSON? That's a jQuery function. As others have said, you don't have valid JSON, so you're never going to get it to parse correctly... Commented Feb 21, 2013 at 18:49
  • Where is this data coming from? Where do you want it to go? Commented Feb 21, 2013 at 18:52

3 Answers 3

5

That isn't JSON it's a Javascript array of objects, not a string. My guess is that you've received this from a jQuery ajax call and you had the dataType : 'json' set so that jQuery has automatically parsed the JSON into this array.

To send it to a PHP script you can convert it back to JSON using:

var myString = JSON.stringify(data);

and then fire off an ajax call to the PHP script with that as the POST data:

var myString = JSON.stringify(data);
$.post('page.php', { data : myString }, function(){
    console.log( "sent" );
});

In PHP you can decode it using:

$data = json_decode($_POST['data']); // <-- or whatever your post variable is named

foreach($data as $obj)
{
    echo $obj->fa_id;
}
Sign up to request clarification or add additional context in comments.

4 Comments

actually you are correct. So how can I make this into an array of objects in PHP then?
@CHiRiLo see my edit. Basically you can convert it back to a string, then ajax it to PHP, then decode on the PHP side.
You could probably forgo the JSON.stringify, and just do $.post('page.php', { data : data }). $_POST['data'] would then just be a an array of arrays.
On javascript side you need to use 'JSON.stringify' and on server side something so that you can parse - like in ruby 'JSON.parse'
0

if you want to get an php array use this

http://php.net/manual/en/function.json-decode.php

Comments

0

The string you provided is not valid JSON.

[Object { fa_id="1167535",  f_id="1000",  loc_type="6",  more...}, 
 Object { fa_id="1167535",  f_id="1000",  loc_type="6",  more...}]

In particular, the "Object" and "more..." strings cannot be interpreted by a JSON parser.

Assuming the object you are inspecting is a variable named foo:

console.log(JSON.stringify(foo));

Should print a valid JSON representation of your object to the Javascript console.

Comments

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.