0
votes

How I can parse JSON response inside my javascript code? After I invoke my worklight adapter, I got this result.

{"linkAccountList": [{
    "accountRule": "1",
    "accountCurrencyISO": "IDR",
    "nickName": "",
    "accountCurrency": "016",
    "accountTypeDisplay": "",
    "accountType": "",
    "accountNo": "1001328000",
    "accountHolderName": "LELY HERNAWATI"
}, {
    "accountRule": "1",
    "accountCurrencyISO": "IDR",
    "nickName": "",
    "accountCurrency": "016",
    "accountTypeDisplay": "",
    "accountType": "",
    "accountNo": "30000000100677",
    "accountHolderName": "LELY HERNAWATI"
}, {
    "accountRule": "1",
    "accountCurrencyISO": "IDR",
    "nickName": "",
    "accountCurrency": "016",
    "accountTypeDisplay": "",
    "accountType": "",
    "accountNo": "2003500382",
    "accountHolderName": "LELY HERNAWATI"
}]
}

Then I store the value inside sessionStorage.

sessionStorage.setItem("linkAccountList", result.invocationResult.linkAccountList);

After that I alert the value, it's not what I wanted even already JSON.stringify() it.

linkAccountList :: "[object Object],[object Object],[object Object]"

What I want is exactly like result above.

My success function

function welcomeSuccess(result){
    WL.Logger.debug("List retrieve success");
    busyIndicator.hide();
    sessionStorage.setItem("linkAccountList", result.invocationResult.linkAccountList);
    WL.Logger.debug("linkAccountList :: " + JSON.stringify(sessionStorage.linkAccountList));
}
1

1 Answers

2
votes

SessionStorage will only store strings. When you put something into SessionStorage, you should stringify it and when getting it back, do a JSON.parse().

function welcomeSuccess(result){
    WL.Logger.debug("List retrieve success");
    busyIndicator.hide();
    sessionStorage.setItem("linkAccountList", JSON.stringify(result.invocationResult.linkAccountList));
    WL.Logger.debug("linkAccountList :: ", JSON.parse(sessionStorage.linkAccountList));
}

What happened in your case was that when it put the value in the SessionStorage, it invoked the toString() method of the array which returned "[object Object],[object Object],[object Object]" and that is what you got back.