Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Json data in Jquery

I am new to Jquery, Ajax and JSON. I am facing issue with the parsing of Json data. I have been through many questions on stackoverflow

Parsing JSON objects for HTML table

Access / process (nested) objects, arrays or JSON

Parse JSON in JavaScript?

How could I parse through this JSON object in JQuery?

and many more...

Still I am not able to parse the Json data.

My Jquery Looks like :

$.ajax({
  /* type : "POST", */
  url : "launchapptest",
  /* contentType: "application/json; charset=utf-8", */
  data : "processDateInput="+processDate,
  dataType : "json",
  async: true,
  success : function(result) {
    var od = JSON.stringify(result) ;
    var obj = JSON.parse(od);

    console.log(obj.od);
    console.log(obj.od.percentageCompleted);

    console.log(od);
    $.each(JSON.parse(od), function(idx, obj) {
      console.log(obj.tagName);
    });         
  }
});

I have tried all the combinations to parse this data, but the js console print as "undefined"

I am able to print the json object as :

{
  "od": [
    {
      "dateProcessed": [
        "09/11/2014",
        "10/11/2014",
        "11/11/2014",
        "12/11/2014"
      ],
      "percentageCompleted": 25,
      "processRunning": 0,
      "remainingTime": 0,
      "successBatchCount": 0,
      "totalBatchCount": 0
    }
  ],
  "processDateInput": "12/11/2014"
}

Please help me how can I fetch dateProcessed array and percentage complete.

like image 256
Tushar Avatar asked Nov 12 '14 09:11

Tushar


People also ask

How can you parse JSON via jQuery?

JQuery | parseJSON() method This parseJSON() Method in jQuery takes a well-formed JSON string and returns the resulting JavaScript value. Parameters: The parseXML() method accepts only one parameter that is mentioned above and described below: json: This parameter is the well-formed JSON string to be parsed.

How does jQuery handle JSON data?

To load JSON data using jQuery, use the getJSON() and ajax() method. The jQuery. getJSON( ) method loads JSON data from the server using a GET HTTP request. data − This optional parameter represents key/value pairs that will be sent to the server.

What is parseJSON in jQuery?

The jQuery parseJSON() method takes a JSON string and returns a JavaScript object. The specified JSON string must follow the strict JSON format. Passing an incorrect string will cause a JS exception. Some of the examples of malformed JSON strings that can cause an exception on passing are given as follows -

How JSON fetch data using Ajax?

The jQuery code uses getJSON() method to fetch the data from the file's location using an AJAX HTTP GET request. It takes two arguments. One is the location of the JSON file and the other is the function containing the JSON data. The each() function is used to iterate through all the objects in the array.


1 Answers

Try this code.

$.ajax({
    /* type : "POST", */
    url: "launchapptest",
    /* contentType: "application/json; charset=utf-8", */
    data: "processDateInput=" + processDate,
    dataType: "json",
    async: true,
    success: function (result) {
        var od = JSON.stringify(result);
        var obj = JSON.parse(od);

        $.each(obj, function (index, value) {
            console.log(obj[index][0].percentageCompleted);
            console.log(obj[index][0].processRunning);
            console.log(obj[index][0].remainingTime);
            console.log(obj[index][0].successBatchCount);
            console.log(obj[index][0].totalBatchCount);
            console.log(obj.processDateInput);
            $.each(obj[index][0].dateProcessed, function (ind, val) {
                console.log(val);
            })
        });

    }
});
like image 75
vanarajcs Avatar answered Oct 01 '22 14:10

vanarajcs