在Ajax Call中解析JSON

时间:2016-01-01 18:21:06

标签: javascript php json ajax

我从数据库的查询中得到了这个JSON响应:

[{
  "IMAGE": "",
  "NAME": "BEANS,LIMA,DRY",
  "NSN": " 8915005302173",
  "FIIG": "T113-J",
  "INC": "2153",
  "CRITICALY": "0",
  "TYPE_II": "4",
  "DEMIL": "a",
  "DATE": "2003-06-12",
  "ESD": ")",
  "PMIC": "a",
  "ADPEC": "0",
  "HMIC": "n",
  "HCC": "",
  "ENAC": "",
  "SUPPLIER": "Defense Supply Agenc",
  "CAGE": "54027",
  "PART_NUMBER": "0",
  "STAT": "a",
  "RNCC": "3",
  "RNVC": "1",
  "DAC": "4",
  "RNAAC": "zz",
  "SADC": "",
  "MSDS": "",
  "MOE": "ds",
  "USC": "i",
  "AAC": "h",
  "SOS": "sms",
  "UI": "lb",
  "UNIT_PRICE": "1",
  "QUP": "1",
  "CIIC": "u",
  "SLC": "0",
  "MGT_CTRL": "",
  "REP": "",
  "SUPPLY_PACKAGE_TYPE": "Fruits and Vegetables",
  "SPECIAL_FEATURES": "",
  "DEFINITION": "Note-Subsistance items which are specifically prepared for dietetic use are classified in Class 8940. Nondietetic foods, even though they bear the same approved item names as corresponding dietetic fo"
}]

我想只选择NSN号码。 这是Ajax调用:

$(document).ready(function(){
function show(){
    $.ajax({
        url:"getProducts",

        success:function(data){
            var d=data;
            $("#output").html(d);
        }
    });
}
show();
});

1 个答案:

答案 0 :(得分:2)

通过添加$.ajax(),告诉JSON方法将您返回的数据视为dataType :'json'。由于您的data将是一个具有数组的对象,该数组在第一个索引处有一个对象,因此您将使用data[0].PROPERTY访问您的属性。例如:

 $.ajax({
    url:"getProducts",
    dataType: 'json',
    success:function(data){
        var d=data[0];
        $("#output").html(d.NSN);
    }
});