嵌套的hashmap对象 - 评估大写的特定值
希望将所有方向值转换为上案例,
或者如果我们有更好的方法在JSON对象本身中,将所有方向值转换为上情况
{
"query": {
"select": {...},
"where": {...},
"orderBy": [
{
"key": "item.id",
"direction": "asc"
},
{
"key": "page.id",
"direction": "desc"
}
]
}
}
尝试的解决方案:
`private static JSONObject uppercaseDirection(JSONObject json) throws JSONException
{
Iterator<?> keys = json.keys();
while (keys.hasNext())
{
String k = (String) keys.next();
if (JSON_PROPERTY_DIRECTION.equals(k))
{
json.put(JSON_PROPERTY_DIRECTION, json.get(JSON_PROPERTY_DIRECTION).toString().toUpperCase());
}
Object value = json.opt(k);
if (value != null && value instanceof JSONObject)
{
uppercaseDirection((JSONObject) value);
} else if ("orderBy".equals(k) && value != null && value instanceof JSONArray)
{
for (Object ob : (JSONArray) value)
{
uppercaseDirection((JSONObject) ob);
}
}
}
return json;
}`
Wanted to convert all direction value to upper case
Or if we have better way to do it in JSON Object itself, converting all direction value to upper case
{
"query": {
"select": {...},
"where": {...},
"orderBy": [
{
"key": "item.id",
"direction": "asc"
},
{
"key": "page.id",
"direction": "desc"
}
]
}
}
Tried Solution:
`private static JSONObject uppercaseDirection(JSONObject json) throws JSONException
{
Iterator<?> keys = json.keys();
while (keys.hasNext())
{
String k = (String) keys.next();
if (JSON_PROPERTY_DIRECTION.equals(k))
{
json.put(JSON_PROPERTY_DIRECTION, json.get(JSON_PROPERTY_DIRECTION).toString().toUpperCase());
}
Object value = json.opt(k);
if (value != null && value instanceof JSONObject)
{
uppercaseDirection((JSONObject) value);
} else if ("orderBy".equals(k) && value != null && value instanceof JSONArray)
{
for (Object ob : (JSONArray) value)
{
uppercaseDirection((JSONObject) ob);
}
}
}
return json;
}`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论是地图还是JSON,基本想法都将是迭代键值对并使用新值更新正确的密钥。
JSON的示例:
输出
Whether it's a map or JSON the basic idea will be to iterate the key-value pairs and update the correct key with the new value.
Example with json:
Output
https://github.com/octomix/josson
https://mvnrepository.com/artifact/artifact/artifact/comcom.octomix.josson.josson/josson/josson/josson/josson
输出
https://github.com/octomix/josson
https://mvnrepository.com/artifact/com.octomix.josson/josson
Output