Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a json object in my typescript which is returned from server side. One of the keys is dynamic. How do I parse and extract the value for that key.

Ex: serverResponse.detailsLMN.allList
Ex :serverResponse.detailsLMN.deleteList

In the above , 'LMN' is dynamic. It can be serverResponse.detailsLMN.allList or serverResponse.detailsPQR.allList.

Assuming, const temp = 'LMN' or 'PQR', how can I use temp to parse JSON object here. Like : serverResponse.details{temp}.allList


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
644 views
Welcome To Ask or Share your Answers For Others

1 Answer

Not sure if I understood your question correctly. But try doing

let data = JSON.parse(serverResponse);

((JSON.stringify(serverResponse)).includes("LMN")) ? serverResponse.detailsLMN.allList 
: serverResponse.detailsPQR.allList 

^The above code is the same as the if statement. If you don't know the ES6 ternary conditional statement then here's a link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

To parse, simply use JSON.parse(). To extract the value, since I can't see the format of the output, it's better to console.log(serverResponse) the whole response and then walk through the Object in Chrome console to see how to get to your specific value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...