Parse Json data in jQuery

I am new to jQuery, Ajax and JSON. I ran into a Json parsing problem. I went through a lot of questions about stackoverflow

Parsing JSON objects for an HTML table

Access / process (nested) objects, arrays or JSON

JSON parsing in JavaScript?

How can I parse this JSON object in jQuery?

and much more...

However, I cannot parse 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 tried all combinations to analyze this data, but the js console prints as "undefined"

I can 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 get a dateProcessed array and the percentage is complete.

+5
source share
4 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); }) }); } }); 
+5
source

When you specify dataType as JSON, jQuery will automatically parse it for you. Repeating it again as you (many times, even) will cause problems. Try the following:

 success: function(result) { console.log(result.od); console.log(result.od[0].percentageCompleted); } 

I'm not quite sure what your $.each trying to do, since there is no tagName property in your object.

+3
source

What is the return data of your AJAX call

like this then

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

you can parse it like this:

 var json = JSON.parse(result); var od = json['od']; var processDateInput = json['processDateInput']; $.each(od, function(index, value){ console.log(value, index); }); 

hope this works for you.

0
source

there is no need to parse it, because you already mentioned it as json, you can just like this:

  success: function(result) { console.log(result.od); console.log(result.od[0].percentageCompleted); console.log(od); $.each(result, function(idx, obj) { console.log(obj[0].dateProcessed); }); } 
0
source

Source: https://habr.com/ru/post/1206704/


All Articles