从php数组中获取解析JSON的值

时间:2014-08-22 05:53:10

标签: javascript php mysql ajax json

$query = "SELECT Item_Name from item_details";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {
    $optionValue[] = "<option value='" . $row['Item_Name'] . "'>" . $row['Item_Name'] . "</option>";
}

$query1 = "SELECT Item_Name from item_addon";
$result1 = mysql_query($query1);

while ($row = mysql_fetch_array($result1)) {
    $optionValue1[] = "<option value='" . $row['Item_Name'] . "'>" . $row['Item_Name'] . "</option>";
}

return json_encode(array("a" => $optionValue, "b" => $optionValue1));

我需要解析ab

的数据
optionDetails = $.parseJSON(data);

for (i = 0; i < optionDetails.length; i++) {
    det_item_name = optionDetails[i];
    $('#det_item_name').append(det_item_name);
}

optionAdd = $.parseJSON(data)
for (i = 0; i < optionAdd.length; i++) {
    det_item_name = optionAdd[i];

    $('#add_name').append(add_name);
}

如何解析aboptionDetailsoptionAdd的数据?

1 个答案:

答案 0 :(得分:1)

尝试这个

var json_array = $.parseJSON(data);
var optionDetails = json_array.a;

for (i = 0; i < optionDetails.length; i++) {
    det_item_name = optionDetails[i];
    $('#det_item_name').append(det_item_name);


}

var optionAdd = json_array.b;
for (i = 0; i < optionAdd.length; i++) {
    det_item_name = optionAdd[i];

    $('#add_name').append(add_name);

}