Java - 如何迭代 json 对象键值对?
Java - How to iterate json objects keys and values pair?
下面是json格式:
Document{
{Social=4, Productivity=5, Personalization=1, Entertainment=5, Music & Audio=4, Finance=7, Tools=9, Travel & Local=1, Food & Drink=3, Card=1, Photography=5, OTHERS=4, Shopping=4, Maps & Navigation=2, Communication=5, Business=3, Video Players & Editors=3
}}
这里我只需要键不需要值。
我期待这样的输出:
{
"name": "Social",
"value": "Social"
},
{
"name": "Productivity",
"value": "Productivity"
}
{
"name": "Personalization",
"value": "Personalization"
}
{
"name": "Entertainment",
"value": "Entertainment"
}
{
"name": "Music & Audio",
"value": "Music & Audio"
}
{
"name": "Finance",
"value": "Finance"
}
{
"name": "Tools",
"value": "Tools"
}
{
"name": "Travel & Local",
"value": "Travel & Local"
}
{
"name": "Food & Drink",
"value": "Food & Drink"
}
{
"name": "Card",
"value": "Card"
}
{
"name": "Photography",
"value": "Photography"
}
{
"name": "OTHERS",
"value": "OTHERS"
}
{
"name": "Shopping",
"value": "Shopping"
}
{
"name": " Maps & Navigation",
"value": " Maps & Navigation"
}
{
"name": "Communication",
"value": "Communication"
}
{
"name": "Business",
"value": "Business"
}
{
"name": "Productivity",
"value": "Productivity"
}
我没有找到合适的解决方案。
for (Document doc : documents) {
doc.entrySet().forEach(v -> {
SegmentValueOptions inputValues = new SegmentValueOptions();
inputValues.setName(v.getKey());
inputValues.setValue(inputValues.getName());
inputValList.add(inputValues);
});
}
}
下面是json格式:
Document{
{Social=4, Productivity=5, Personalization=1, Entertainment=5, Music & Audio=4, Finance=7, Tools=9, Travel & Local=1, Food & Drink=3, Card=1, Photography=5, OTHERS=4, Shopping=4, Maps & Navigation=2, Communication=5, Business=3, Video Players & Editors=3
}}
这里我只需要键不需要值。 我期待这样的输出:
{
"name": "Social",
"value": "Social"
},
{
"name": "Productivity",
"value": "Productivity"
}
{
"name": "Personalization",
"value": "Personalization"
}
{
"name": "Entertainment",
"value": "Entertainment"
}
{
"name": "Music & Audio",
"value": "Music & Audio"
}
{
"name": "Finance",
"value": "Finance"
}
{
"name": "Tools",
"value": "Tools"
}
{
"name": "Travel & Local",
"value": "Travel & Local"
}
{
"name": "Food & Drink",
"value": "Food & Drink"
}
{
"name": "Card",
"value": "Card"
}
{
"name": "Photography",
"value": "Photography"
}
{
"name": "OTHERS",
"value": "OTHERS"
}
{
"name": "Shopping",
"value": "Shopping"
}
{
"name": " Maps & Navigation",
"value": " Maps & Navigation"
}
{
"name": "Communication",
"value": "Communication"
}
{
"name": "Business",
"value": "Business"
}
{
"name": "Productivity",
"value": "Productivity"
}
我没有找到合适的解决方案。
for (Document doc : documents) {
doc.entrySet().forEach(v -> {
SegmentValueOptions inputValues = new SegmentValueOptions();
inputValues.setName(v.getKey());
inputValues.setValue(inputValues.getName());
inputValList.add(inputValues);
});
}
}