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.
asked Apr 22, 2013 at 17:56
user1596433
66910 silver badges17 bronze badges
2 Answers 2
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/
answered Apr 22, 2013 at 17:58
Ian
51k13 gold badges104 silver badges111 bronze badges
Sign up to request clarification or add additional context in comments.
6 Comments
mellamokb
+1 Also, you can use
obj['BILLINGINFO'] and obj['DROPDOWN'].Ian
@mellamokb True, but stackoverflow.com/questions/4968406/… . I'll still add it in
user1596433
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>'); });
Ian
@user1596433 Why are you using
data.dropdowns? It should be data.DROPDOWNSuser1596433
Thank You Ian, that was the issue. I was so dumb to not to notice that.
|
You can use
$.parseJson
function to parse it
answered Apr 22, 2013 at 17:57
chandresh_cool
11.9k3 gold badges32 silver badges46 bronze badges
2 Comments
user400654
@hop We have no way of knowing whether or not that is parsed yet due to lack of information.
hop
@KevinB Fine.But, does this answer the question
lang-js