-4

How can I extract the following JSON in jQuery and show it in an alert box?

{
  "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
  }],
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 2
    Firstly, what you have is an object, not JSON so you can retrieve the data as you normally would from an object. Secondly, jQuery is useless here. It's for amending the DOM, not reading from an object. Thirdly, exactly what part of this are you wanting to show in an `alert()`? – Rory McCrossan Sep 15 '17 at 08:15
  • 2
    Fourthly, what have you tried so far and which part are you having difficulty with? – freedomn-m Sep 15 '17 at 08:18
  • i want the result in alert as (चुना,सल्फर,जिप्सम,फॉस्फरिक एसिड,सल्फुरिक एसिड,मल्चिंग ) show it in alert to user – MANGESH UKADE Sep 15 '17 at 08:19
  • $.getJSON("my url", function (data) { var names = ata.map(function (value) { return alue.Data[0].PARAMETER; }); alert("Successfully egister" + names + "----------------------"); ; – MANGESH UKADE Sep 15 '17 at 08:22
  • 1
    low quality question – HD.. Sep 15 '17 at 08:26
  • @RoryMcCrossan just wondering why this isn't `JSON`. It looks like it meets all the requirements to me but I must be missing something - https://stackoverflow.com/questions/3975859/what-are-the-differences-between-json-and-javascript-object – Paul Fitzgerald Sep 15 '17 at 08:37
  • 1
    @PaulFitzgerald JSON is a format to serialise a string. People often use 'JSON object' (or 'JSON array'), when what they mean is 'An object deserialised from JSON'. In other words, at the point you work with the data, it is an object and the fact that it was ever serialised in to JSON is irrelevant. – Rory McCrossan Sep 15 '17 at 08:38
  • @Paul Fitzgerald it's a perfectly valid `JSON object`, maybe he was referring to a `JSON string` ? – anteAdamovic Sep 15 '17 at 08:38
  • how I can get the result from above code as (चुना,सल्फर,जिप्सम,फॉस्फरिक एसिड,सल्फुरिक एसिड,मल्चिंग ) in alert – MANGESH UKADE Sep 15 '17 at 08:44

2 Answers2

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

Blue
  • 312
  • 2
  • 12
0

Is this the sort of thing that you are after?

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

Or if you want to be fancy

var example_json='{"ID": null,"ResponseCode": "0"}'; 
alert(JSON.stringify(JSON.parse(example_json)));
Alexx Roche
  • 3,151
  • 1
  • 33
  • 39