0

Following is my JSON output :

{
    "BILLINGINFO": [
        {
            "CUST_REQ_BILL_DATE": "15",
            "BILL_MONTH": "03",
            "CONSOLIDATION_CRITERIA": "016",
            "CONSOLIDATION_OPTION": "A",
            "SPLIT_LINES": "",
            "BILL_IN_ARREARS": "X",
            "BILL_CREATE_DATE": "02"
        }
    ],
    "DROPDOWNS": [
        {
            "FIELD": "CUST_REQ_BILL_DATE",
            "VALUE": "01",
            "TEXT": "1st of month"
        },
        {
            "FIELD": "CUST_REQ_BILL_DATE",
            "VALUE": "02",
            "TEXT": "2nd of month"
        }
   ]
}

I am still learming jquery and not sure how to retrive values of BILLINGINFO and DROPDOWN arrays.

user1596433
  • 629
  • 9
  • 17

2 Answers2

3

Say this JSON is stored in a variable called obj. Then you'd use:

obj.BILLINGINFO
// and
obj.DROPDOWNS

or:

obj["BILLINGINFO"]
obj["DROPDOWNS"]

Reference: JavaScript property access: dot notation vs. brackets?

To loop through them, you can use something like the following (need to apply to each):

for (var i = 0; i < obj.BILLINGINFO.length; i++) {
    var current = obj.BILLINGINFO[i];
    // Work with `current` and you can use
    // current.CUST_REQ_BILL_DATE, current.BILL_MONTH, etc.
}

So there's no need to use jQuery for any of this. But an option for looping is using each: http://api.jquery.com/jQuery.each/

Community
  • 1
  • 1
Ian
  • 50,146
  • 13
  • 101
  • 111
  • +1 Also, you can use `obj['BILLINGINFO']` and `obj['DROPDOWN']`. – mellamokb Apr 22 '13 at 17:58
  • @mellamokb True, but http://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets . I'll still add it in – Ian Apr 22 '13 at 18:00
  • Actually, the dropdown array is meant for combobox values and I need to loop thru it. I am using this but it gives "cannot read property 'length' of undefined" error :   $.each(data.dropdowns, function(index) { $('#conscrit').append(''); }); – user1596433 Apr 22 '13 at 18:06
  • 1
    @user1596433 Why are you using `data.dropdowns`? It should be `data.DROPDOWNS` – Ian Apr 22 '13 at 18:13
  • Thank You Ian, that was the issue. I was so dumb to not to notice that. – user1596433 Apr 22 '13 at 18:21
  • @user1596433 No problem, basically everything's case-sensitive in Javascript, and this is no exception :) – Ian Apr 22 '13 at 18:22
1

You can use

$.parseJson

function to parse it

chandresh_cool
  • 11,753
  • 3
  • 30
  • 45