嵌套的hashmap对象 - 评估大写的特定值

发布于 2025-02-13 08:23:06 字数 1468 浏览 1 评论 0原文

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;
}`

Below mentioned HashMap

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单身狗的梦 2025-02-20 08:23:07

无论是地图还是JSON,基本想法都将是迭代键值对并使用新值更新正确的密钥。

JSON的示例:

String json = "{\"query\":{\"select\":{\"f1\":1},\"where\":{\"f1\":1},\"orderBy\":[{\"key\":\"item.id\",\"direction\":\"asc\"},{\"key\":\"page.id\",\"direction\":\"desc\"}]}}";
JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
JsonArray jsonArray = jsonObject.get("query").getAsJsonObject().get("orderBy").getAsJsonArray();
Iterator<JsonElement> itr = jsonArray.iterator();
while(itr.hasNext()) {
    JsonObject obj = itr.next().getAsJsonObject();          
    obj.addProperty("direction", obj.get("direction").getAsString().toUpperCase());
}

System.out.println(jsonObject);

输出

{"query":{"select":{"f1":1},"where":{"f1":1},"orderBy":[{"key":"item.id","direction":"ASC"},{"key":"page.id","direction":"DESC"}]}}

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:

String json = "{\"query\":{\"select\":{\"f1\":1},\"where\":{\"f1\":1},\"orderBy\":[{\"key\":\"item.id\",\"direction\":\"asc\"},{\"key\":\"page.id\",\"direction\":\"desc\"}]}}";
JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
JsonArray jsonArray = jsonObject.get("query").getAsJsonObject().get("orderBy").getAsJsonArray();
Iterator<JsonElement> itr = jsonArray.iterator();
while(itr.hasNext()) {
    JsonObject obj = itr.next().getAsJsonObject();          
    obj.addProperty("direction", obj.get("direction").getAsString().toUpperCase());
}

System.out.println(jsonObject);

Output

{"query":{"select":{"f1":1},"where":{"f1":1},"orderBy":[{"key":"item.id","direction":"ASC"},{"key":"page.id","direction":"DESC"}]}}
南汐寒笙箫 2025-02-20 08:23:07

https://github.com/octomix/josson
https://mvnrepository.com/artifact/artifact/artifact/comcom.octomix.josson.josson/josson/josson/josson/josson

Josson josson = Josson.fromJsonString("{\"query\":{\"select\":{\"f1\":1},\"where\":{\"f1\":1},\"orderBy\":[{\"key\":\"item.id\",\"direction\":\"asc\"},{\"key\":\"page.id\",\"direction\":\"desc\"}]}}");
JsonNode node = josson.getNode("field(query.field(orderBy.field(direction.upperCase())))");
System.out.println(node.toPrettyString());

输出

{
  "query" : {
    "select" : {
      "f1" : 1
    },
    "where" : {
      "f1" : 1
    },
    "orderBy" : [ {
      "key" : "item.id",
      "direction" : "ASC"
    }, {
      "key" : "page.id",
      "direction" : "DESC"
    } ]
  }
}

https://github.com/octomix/josson
https://mvnrepository.com/artifact/com.octomix.josson/josson

Josson josson = Josson.fromJsonString("{\"query\":{\"select\":{\"f1\":1},\"where\":{\"f1\":1},\"orderBy\":[{\"key\":\"item.id\",\"direction\":\"asc\"},{\"key\":\"page.id\",\"direction\":\"desc\"}]}}");
JsonNode node = josson.getNode("field(query.field(orderBy.field(direction.upperCase())))");
System.out.println(node.toPrettyString());

Output

{
  "query" : {
    "select" : {
      "f1" : 1
    },
    "where" : {
      "f1" : 1
    },
    "orderBy" : [ {
      "key" : "item.id",
      "direction" : "ASC"
    }, {
      "key" : "page.id",
      "direction" : "DESC"
    } ]
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文