Stack Overflow Asked by FranceMadrid on January 1, 2022
In my Json file I have the following field:
"categories": [{
"name1": "Corporate",
"parent": {
"name_parent": "PlanType1"
}
}],
In my Java file I have this code to access that json variable:
PlanType.getnewEntity().getCategories().
The .getCategories()
is accessing the "categories"
json variable, I just am having trouble iterating through "categories"
In my code I need the logic that if in "categories"
if name_parent = "PlanType1"
AND name1 = "Corporate"
do x. I am just having trouble constructing that if statement by iterating through the json.
Try this. I don't actually know how the JSON variable you are getting. But it would work if u cast it into a List of Maps. For example
List<Map<String, Object>> catogoriesList = (List<Map<String, Object>>) PlanType.getnewEntity().getCategories();
Then you'll be able to iterate through it like as follows.
for(Map<String, Object> catogory : catogoriesList){
Map<String, Object> parent = (Map<String, Object>) catogory.get("parent");
if(catogory.get("name1").toString().equals("Corporate") && parent.get("name_parent").toString().equals("PlanType1")){
// Do x here
}
}
Answered by Althaf1467 on January 1, 2022
You can iterate like below
categories.getCategories().forEach(
category -> {
if("Corporate".equals(category.getName1()) &&
("PlanType1".equals(category.getParent().getNameParent()))) {
//do the logic
}
}
);
Answered by mgeor on January 1, 2022
3 Asked on January 17, 2021 by soumya-ranjan-sahoo
1 Asked on January 17, 2021 by abi-crazieeee
django django queryset django rest framework django serializer serialization
1 Asked on January 17, 2021 by 0x4b50
4 Asked on January 17, 2021 by ds_
3 Asked on January 17, 2021 by user3093820
1 Asked on January 17, 2021
2 Asked on January 17, 2021 by nicole-aragon
2 Asked on January 17, 2021 by paul-davis
1 Asked on January 16, 2021 by toniq
1 Asked on January 16, 2021 by victor-molina
1 Asked on January 16, 2021 by mathew-nhari
2 Asked on January 16, 2021 by jrmy-gachon
3 Asked on January 16, 2021 by user118967
2 Asked on January 16, 2021 by oma11
3 Asked on January 16, 2021 by ninita
0 Asked on January 16, 2021 by that1guy
4 Asked on January 16, 2021 by waiwai57
Get help from others!
Recent Questions
Recent Answers
© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP