Skip to content
Advertisement

JSON – how to parse it?

I just got a problem about this response. Supposedly if I want to choose item from the response it must be an array value.

"telegraphicTransferBankMap": {
        "BOA BEJ, JKT": "0330301",
        "BPD KALBAR SYARIAH, PTK": "1239922",
        "ANDARA, DPSR": "4660019",
        "GANESHA HAYAM WURUK, JKT": "1610017",
...
}

Actually must be like this one right? (let me know if I’m mistaken)

"telegraphicTransferBankMap": [
            {"BOA BEJ, JKT": "0330301"},
            {"BPD KALBAR SYARIAH, PTK": "1239922"},
            {"ANDARA, DPSR": "4660019"},
            {"GANESHA HAYAM WURUK, JKT": "1610017"},
    ...
    ]

My code:

var ul = $('#telegraphicTransferBankMap');
    var html ='';
    for (var key in telegraphicTransferBankMap) {
        if (telegraphicTransferBankMap.hasOwnProperty(key)) {
            telegraphicTransferBankMapItem = telegraphicTransferBankMap[key];
            html += '<option value="'+telegraphicTransferBankMapItem.xxxxxx+'">'+telegraphicTransferBankMapItem.yyyyyyy+'</option>';
        }
    }
    ul.append(html);

Then I want to put telegraphicTransferBankMapItem.xxxxxx as bankId and telegraphicTransferBankMapItem.yyyyyyy as bankName.

Advertisement

Answer

Use your first json and change your loop like,

var telegraphicTransferBankMap = {
    "BOA BEJ, JKT": "0330301",
    "BPD KALBAR SYARIAH, PTK": "1239922",
    "ANDARA, DPSR": "4660019",
    "GANESHA HAYAM WURUK, JKT": "1610017",
}

for (var key in telegraphicTransferBankMap) {
    if (telegraphicTransferBankMap.hasOwnProperty(key)) {
        bank = telegraphicTransferBankMap[key];
        html += '<option value="' + bank + '">' + key + '</option>';
    }
}

Live Demo

Also you should bank-id as key in your json rather than of bank name.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement