admin管理员组

文章数量:1419192

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?

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?

Share Improve this question asked Mar 5, 2013 at 13:57 vulkoingimvulkoingim 1171 gold badge4 silver badges11 bronze badges 1
  • 1 That's an array [] containing one object {}. You must access the first element of the array parseJSON(res)[0] – Michael Berkowski Commented Mar 5, 2013 at 14:01
Add a ment  | 

5 Answers 5

Reset to default 4

Try:

var r = $.parseJSON(res);

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

});

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

You can get some kind of object from json:

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

Try this:

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

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

});

Hope this will help you

本文标签: javascriptGetting key gt value from json array passed from phpStack Overflow