1

I am trying to get an array from PHP and to manipulate it further using jQuery. In my PHP file i do echo json_encode($data) and when i put an alert in my response in jQuery i get:

[
    {
        "CustomerID": "C43242421",
        "UserID": "432421421",
        "Customer": "rqewrqwreeqwr",
        "Add1": "rqwerqwreqwrqwrqwr",
        "Add2": " ",
        "Add3": " ",
        "Phone": "4131231",
        "Fax": "532442141",
        "Contact": "reqwrqwrw",
        "Email": "gfdgdsg",
        "PaymentTerm": null,
        "Country": "3231",
        "City": "111",
        "Zip": " "
    }
]

, wich is a valid json array. Now what i try to do further is get the pairs as key => value as i would in an associative array in php.

$.post("templates/test.php",
    {data: query,
     cond: $(this).text(),
     action: 'select'
     },
function(res) {
    alert(res) //outputs what i pasted above
    $.each($.parseJSON(res), function(key, value) {
        alert(key + value);
        //this outputs: 0[object Object]
});

Removing the $.parseJSON in the above function gives me a invalid 'in' operand e on jquery.min.js(line 3) in Firebug error log.Can you assist me with my troubles?

1
  • 1
    That's an array [] containing one object {}. You must access the first element of the array parseJSON(res)[0] Commented Mar 5, 2013 at 14:01

5 Answers 5

4

Try:

var r = $.parseJSON(res);

$.each(r[0], function(key, value) {
        alert(key + value);

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

1 Comment

Yep, this works. Thank you very much! I tried directly putting $.parseJSON(res[0]); but i got JSON.parse: unexpected end of data and didn't try it any other way :) Thanks again! And now i see i had to do it like $.parseJSON(res)[0]; :D
1

The result of $.parseJSON(res) is an array, containing a single element (an object). When you iterate over that array (using $.each), value represents the entire object that's stored at the current index of the array. You'll need to iterate over that object to output its properties:

$.each($.parseJSON(res)[0], function(key, value) {
    alert(key + ' = ' + value);
});

If you have an array with multiple objects inside it, this more general code should output the key-value pairs for all of them:

$.each($.parseJSON(res), function(index, arrayObject) {
    $.each(arrayObject, function(key, value) {
        alert(key + ' = ' + value);
    });
});

Comments

0
res = $.parseJSON(res);

for (var i = 0; l = res.length; i < l; i++) {
   data = res[i];
   customer = data.Customer;

}

You have there an Array of Objects. You can iterate through the array of objects just like the code above.

Comments

0

You can get some kind of object from json:

function parse_json(res)
{
  try{
    return eval('(' + response + ')');
  }
  catch(e){
    // invalid json
  }
}

1 Comment

This isn't relevant to the question at all, since they've already converted the JSON string to the object it represents (that's what $.parseJSON() does).
0

Try this:

$.getJSON('your-json-string-file.php', function (data) {

  $.each(data, function(key, val) {
    alert(key +'=>'+ val)
  });

});

Hope this will help you

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.