如何在jQuery中从JSON中提取数据

时间:2017-09-15 08:13:06

标签: javascript jquery arrays

如何在jQuery中提取以下JSON并将其显示在警告框中?

{
  "ID": null,
  "ResponseCode": "0",
  "ResponseMessage": "Success...",
  "Data": [{
    "PARAMETER": "चुना",
    "QUANTITY": "500",
    "UNIT": "किलो",
    "METHOD": null
  }, {
    "PARAMETER": "सल्फर",
    "QUANTITY": "20",
    "UNIT": "किलो",
    "METHOD": null
  }, {
    "PARAMETER": "जिप्सम",
    "QUANTITY": "0",
    "UNIT": "MT",
    "METHOD": null
  }, {
    "PARAMETER": "फॉस्फरिक एसिड",
    "QUANTITY": "16",
    "UNIT": "किलो",
    "METHOD": null
  }, {
    "PARAMETER": "सल्फुरिक एसिड",
    "QUANTITY": "4",
    "UNIT": "किलो",
    "METHOD": null
  }, {
    "PARAMETER": "मल्चिंग ",
    "QUANTITY": "मल्चिंग",
    "UNIT": "-",
    "METHOD": null
  }],
}

2 个答案:

答案 0 :(得分:1)

var data = [
{
  "ID": null,
  "ResponseCode": "0",
  "ResponseMessage": "Success...",
  "Data": [{
    "PARAMETER": "चुना",
    "QUANTITY": "500",
    "UNIT": "किलो",
    "METHOD": null
  }, {
    "PARAMETER": "सल्फर",
    "QUANTITY": "20",
    "UNIT": "किलो",
    "METHOD": null
  }, {
    "PARAMETER": "जिप्सम",
    "QUANTITY": "0",
    "UNIT": "MT",
    "METHOD": null
  }, {
    "PARAMETER": "फॉस्फरिक एसिड",
    "QUANTITY": "16",
    "UNIT": "किलो",
    "METHOD": null
  }, {
    "PARAMETER": "सल्फुरिक एसिड",
    "QUANTITY": "4",
    "UNIT": "किलो",
    "METHOD": null
  }, {
    "PARAMETER": "मल्चिंग ",
    "QUANTITY": "मल्चिंग",
    "UNIT": "-",
    "METHOD": null
  }]
}
];

 var vData="";

 $.each(data, function(index, element) {             
     $.each(element.Data, function(index2, element2) {
         vData+=element2.PARAMETER+" "; 
     }); 
 });

 alert(vData);

See this demo

答案 1 :(得分:0)

这是你想要的那种东西吗?

var example_json='{"ID": null,"ResponseCode": "0"}'; 
alert(JSON.stringify(example_json));

或者如果你想要花哨

var example_json='{"ID": null,"ResponseCode": "0"}'; 
alert(JSON.stringify(JSON.parse(example_json)));