0

usually, I'm using data from my own MySQL but my company decided to use third party API which produce this JSON data :

{"ongkir":{
"query":{"city":"444"},
"status":{"code":200,"description":"OK"},
"results":[{"subdistrict_id":"6131","province_id":"11","province":"Jawa Timur","city_id":"444","city":"Surabaya","type":"Kota","subdistrict_name":"Asemrowo"},...

and I have this jquery :

$.each(json, function(i,o){
subdistric += "<option value="+o.subdistrict_id+">"+o.subdistrict_name+"</option>";});

and it fails to display the <option> because the JSON data contains prefix which actually I don't need. how get subdistrict_id and subdistrict_name from "results" part of JSON using jquery?

thank you very much for your help

asked Mar 28, 2016 at 8:43
1
  • Does key ongkir is common or change? Commented Mar 28, 2016 at 8:47

2 Answers 2

1

Try

$.each(json.ongkir.results, function(i,o){
subdistric += "<option value="+o.subdistrict_id+">"+o.subdistrict_name+"</option>";});
answered Mar 28, 2016 at 8:44
Sign up to request clarification or add additional context in comments.

1 Comment

works like a charm, bro!! thanks a lot! i will accept this as an answer
1

It will be good if you put the html in a variable. Later when you want to add other attributes to it will be easy and code is more readable than string concatenation.

var template = "<option value='#VALUE'>#NAME</option>";
$.each(json.ongkir.results, function(i,o){
 subdistric += template.replace("#VALUE", o.subdistrict_id).replace("#NAME",o.subdistrict_name);
});
answered Mar 28, 2016 at 8:50

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.