Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

javascript - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
            })
        });

    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...