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.

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

6 Comments

+1 Also, you can use obj['BILLINGINFO'] and obj['DROPDOWN'].
@mellamokb True, but stackoverflow.com/questions/4968406/… . I'll still add it in
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('<option>' + data.dropdowns[index].TEXT + '</option>'); });
@user1596433 Why are you using data.dropdowns? It should be data.DROPDOWNS
Thank You Ian, that was the issue. I was so dumb to not to notice that.
|
1

You can use

$.parseJson

function to parse it

2 Comments

@hop We have no way of knowing whether or not that is parsed yet due to lack of information.
@KevinB Fine.But, does this answer the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.