最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parse Json data in Jquery - Stack Overflow

programmeradmin1浏览0评论

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 binations 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 plete.

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 binations 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 plete.

Share Improve this question edited May 23, 2017 at 12:16 CommunityBot 11 silver badge asked Nov 12, 2014 at 9:45 TusharTushar 1,4706 gold badges18 silver badges30 bronze badges 1
  • when I am trying for each loop, the lop is running twice even though there is only onw record... please help – Tushar Commented Nov 12, 2014 at 11:28
Add a ment  | 

4 Answers 4

Reset to default 6

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

    }
});

When you specify the dataType as JSON, jQuery will automatically parse it for you. Parsing it again as you are (multiple times, even) will cause issues. Try this:

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

I'm not entirely sure what your $.each loop is trying to do as there is no tagName property in your object.

What is the is the return data of your AJAX call

is 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 it would work on you.

no need of parsing it because you have already mentioned it as json you can simply do 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)

  1. 暂无评论