John
John

Reputation: 13

Parse JSON Object in Ajax

Following is the ajax code :

$.ajax({
    url: "update.php",
    type: "post",
    dataType: "json",
    data: {"editId":editId},
    success: function (data,status) {
        //console.log(data);
    },
    error: function(response) {
        console.log(response);
    },
});

And getting result as: 0: Object { 0: "2", 1: "JOHN", 2: "2147483647", … } How to parse this data

Upvotes: 2

Views: 833

Answers (4)

bitifet
bitifet

Reputation: 3669

As others said:

  1. Technically you don't need to parse anything because that already is a valid JSON object.

  2. That seems to come from, maybe not properly, converted to JSON PHP object.

    • In fact it seems to pretend to be a "real"⁽¹⁾ Array.

      (1) Note that PHP call "Arrays" what in Javascript we call "Objects" and, if they happen to only (or, at your own risk, even if not only) contain numeric keys, they can be treated as "real" arrays. So, by "real" array, I mean arrays having only numeric keys so they can be converted to JSON arrays.

So I guess what you really want is to get an actual JSON array insetad of simple object.

If I'm right in that, possible solutions are:

SERVER SIDE (Best solution):

If you have chance, I would recommend to fix server side code instead.

I mean: If, as I guess, your data is expected to be a real array with only numeric keys, then convert to real JSON array.

But, in fact, this is what json_encode() php function is expected to do by default if possible:

<?php
echo json_encode(Array(1,2, 3, 4, "foo") . "\n");
// [1,2,3,4,"foo"]
echo json_encode(Array(1,2, 3, 4, "foo"=>"bar") . "\n");
// {"0":1,"1":2,"2":3,"3":4,"foo":"bar"}

So there should be some circumstance underneath that avoids it. Say:

  • Data has non numeric keys you haven't shown in your example.

    • In that case you are forced to use that non-array form or make some more specific transformation (like ignoring keys and just consider object values).
  • Your PHP API isn't using json_encode() for conversion or is using (maybe as a generic pattern to avoid unconsistent response format if there can be results with or without non-numeric keys) JSON_FORCE_OBJECT modifier.

Example:

<?php
echo json_encode(Array(1,2, 3, 4, "foo"), JSON_FORCE_OBJECT) . "\n";
// {"0":1,"1":2,"2":3,"3":4,"4":"foo"

CLIENT SIDE:

On the other hand, if you can't (or, for any reason, you don't want to) modify server side code you can convert it back to an array with javascript client-side.

Example:

var foo = { 0: "2", 1: "JOHN", 2: "2147483647"};
console.log(Object.values(foo));
// [ '2', 'JOHN', '2147483647' ]

Upvotes: 1

godot
godot

Reputation: 3545

Your response is already in JSON format and when you write in your ajax call dataType: 'json' you can treat your response object as javascript object. So if in your JSON keys are numbers you can get them like that:

 success: function (response,status) {
        data = response[0];
        console.log(data[0]); // logs 2
        console.log(data[1]); // logs "JOHN"
    },

when your php response looks like that:

echo json_encode(array(0=>array(0=> "2", 1=> "JOHN", 2=> "2147483647")));

Upvotes: 2

Saswat
Saswat

Reputation: 12836

The response you are getting 0: Object { 0: "2", 1: "JOHN", 2: "2147483647", … } suggests that you are encoding a php object into json, which you should avoid. The 0, 1 and 2 are the phpobject index. You need to create an associative array with understandable "keys".

You should form an array first. For example if you are fetching php object, you need to convert it to array in your php file like this:-

$arrayList = array();
foreach($phpOb as $po) // whatever be the object
{
     $arr['id'] = $po->id;
     $arr['name'] = $po->name;
     $arr['roll'] = $po->roll;
     $arrayList[] = $arr;
}

Then, you need to send the arrayList by encoding it as json.

echo json_encode($arrayList);

Upvotes: 1

user3582495
user3582495

Reputation: 11

If your content-type is application/json (what I guess) you don't have to. It is already a JS object. If not, you could use JSON.parse(data).

Upvotes: 0

Related Questions