-3

I am new to Jquery. I am calling a Struts 2 action which return a Json object. I dont understand how actually .each function works in Jquery.

Can you please explain how can I use the variable data, in a complex json using jquery.

{
    "od": {
        "cwd": [
            {
                "batchCount": 140,
                "batchId": "2121",
                "countryName": "Mexico",
                "processId": "210002",
                "status": "F",
                "timeRequired": 140
            },
            {
                "batchCount": 140,
                "batchId": "8259",
                "countryName": "Japan",
                "processId": "220002",
                "status": "F",
                "timeRequired": 140
            }           
        ],
        "percentageCompleted": 100,
        "remainingTime": "-104Hours -4Mins",
        "successBatchCount": 0,
        "totalBatchCount": 920
    },
    "processDateInput": "19/11/2014" }

So here is what I wanted to know that if json data is parsed into var obj,

Can cwd be accessed by :

var cwd = result.od.cwd;
$.each(cwd, fuction(index, value)){
var batchcount = value.batchcount;
});

and likewise can we parse any json string in jquery.

Thanks and regards, Tushar

Tushar
  • 1,450
  • 6
  • 18
  • 30
  • 2
    Have you gone through docs? What did you didn't understood? – Satpal Nov 24 '14 at 06:53
  • yes, but docs explain the concept using simple data variables, I have a bit complex json data, so not sure how to manage multiple loops. – Tushar Nov 24 '14 at 06:55
  • 1
    You've used it before, like in your code in [this question](http://stackoverflow.com/questions/26883760/parse-json-data-in-jquery). So if you have a specific problem, you need to ask about it. – cookie monster Nov 24 '14 at 06:55
  • Why do you think you even need `.each`? What are you trying to do? – Felix Kling Nov 24 '14 at 06:59
  • please give us complete details for your query your question is incomplete so complete it .Then surely you may receive correct feedback. – Ricky Nov 24 '14 at 07:05
  • I have edited the same, with exact issue, can you please help now. – Tushar Nov 24 '14 at 07:20

1 Answers1

3

For basic understanding , explain you with your example,

First parent object has two object , "od" and "processDateInput" you can get the values of object directly like od.percentageCompleted , od.remainingTime, processDateInput etc..

So if there is an array inside an object then you need to go for $.each or for loop ,example

$.each(od.cwd,function(index,element){
     /*index will be 0 and element will have first cwd[0] value {
            "batchCount": 140,
            "batchId": "2121",
            "countryName": "Mexico",
            "processId": "210002",
            "status": "F",
            "timeRequired": 140
        }*/
     //similar to this you can get the value
     var batchCount=element.batchCount;
});