简体   繁体   English

通用 Json Map Java 中的二传手

[英]Generic Json Map Setter in Java

Suppose I have an empty Map<String, Object> map or a Map<String, Object> map with some fields added.假设我有一个空的 Map<String, Object> map 或一个添加了一些字段的 Map<String, Object> map。

In this case, I want to create a generic java method that I can set to the map I have using the values such as map, path, value type and value object that I will give as input.在这种情况下,我想创建一个通用的 java 方法,我可以使用 map、路径、值类型和值 object 等值将其设置为 map 作为输入。

The method outline I intend to create can be seen below.我打算创建的方法大纲如下所示。

public void genericJsonMapSet(Map<String, Object> jsonMap, String setPath, String setValueType, Object setValue) {
    //Set JsonMap code
}

The json representation of the map before and after the following calls of the method to be created should be as follows. json map 的以下调用前后要创建的方法的表示应如下所示。

    genericJsonMapSet(jsonMap, "setid()", "string", "a9a9940d-593f-4546-8065-6d70370d9f64");
    genericJsonMapSet(jsonMap, "setrequestDate()", "string", "2022-10-10 16:20:15");
    genericJsonMapSet(jsonMap, "setuserCharacteristic[1].setvalueType()", "string", "string");
    genericJsonMapSet(jsonMap, "setuserCharacteristic[1].setname()", "string", "username");
    genericJsonMapSet(jsonMap, "setuserCharacteristic[1].setvalue()", "string", "taner.turan");
    genericJsonMapSet(jsonMap, "setuserCharacteristic[2].setvalueType()", "string", "integer");
    genericJsonMapSet(jsonMap, "setuserCharacteristic[2].setname()", "string", "id");
    genericJsonMapSet(jsonMap, "setuserCharacteristic[2].setvalue()", "integer", "23");

Before:前:

{

}

After:后:

{
    "id": "a9a9940d-593f-4546-8065-6d70370d9f64",
    "requestDate": "2022-10-10 16:20:15",
    "userCharacteristic": [
        {
            "valueType": "string",
            "name": "username",
            "value": "taner.turan"
        },
        {
            "valueType": "integer",
            "name": "id",
            "value": 23
        }
    ]
}

Can anyone provide code for this?任何人都可以为此提供代码吗?

This project works similarly.这个项目的工作原理类似。 I wrote it.. a long long time ago, as far as I know nobody uses it.我写的..很久以前,据我所知没有人使用它。 Map<String, Object> is not java-like at all; Map<String, Object>一点也不像 java; there is almost no purpose to such a thing.这样的事情几乎没有任何目的。 I encapsulated the notion into a JSON object instead.我将这个概念封装到JSON object 中。 Also, you don't use strings to 'set' things, again, un-java like.此外,您不使用字符串来“设置”事物,同样,非 Java 之类的。 Thus, this code:因此,这段代码:

 Map<String, Object> jsonMap = new HashMap<>(); genericJsonMapSet(jsonMap, "setid()", "string", "a9a9940d-593f-4546-8065-6d70370d9f64"); genericJsonMapSet(jsonMap, "setuserCharacteristic[1].setvalueType()", "string", "string"); genericJsonMapSet(jsonMap, "setuserCharacteristic[2].setvalue()", "integer", "23");

Becomes:变成:

JSON j = JSON.newMap();
j.get("setId").setString("a9a9940d-593f-4546-8065-6d70370d9f64");
j.get("setuserCharacteristic").add().setString("string");
j.get("setuserCharacteristic").get(2).setInteger(23);

add() is the same as .get(i) where i is one larger than whatever is there already. add().get(i)相同,其中 i 比现有的任何内容大 1。 You can get non-existent things;get不存在的东西; they are created automatically once you .set anywhere in the 'chain'.一旦您在“ .set ”中的任何位置进行设置,它们就会自动创建。 ie: IE:

JSON j = JSON.newMap();
j.get("foo").get("bar").setString("hello");

Gives you JSON structure:给你 JSON 结构:

{
  "foo": {
    "bar": "hello"
  }
}

Link to code on github链接到 github 上的代码

You can make use of the function unflatten() of library Josson to do what you want.您可以使用 Josson 库的function unflatten()来执行您想要的操作。

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

Josson josson = Josson.create();
josson.put("id", "a9a9940d-593f-4546-8065-6d70370d9f64");
josson.put("requestDate", "2022-10-10 16:20:15");
josson.put("userCharacteristic[1].valueType", "string");
josson.put("userCharacteristic[1].name", "username");
josson.put("userCharacteristic[1].value", "taner.turan");
josson.put("userCharacteristic[2].valueType", "integer");
josson.put("userCharacteristic[2].name", "id");
josson.put("userCharacteristic[2].value", 23);
JsonNode node = josson.getNode("unflatten('.[]')");
System.out.println(node.toPrettyString());

Output Output

{
  "id" : "a9a9940d-593f-4546-8065-6d70370d9f64",
  "requestDate" : "2022-10-10 16:20:15",
  "userCharacteristic" : [ {
    "valueType" : "string",
    "name" : "username",
    "value" : "taner.turan"
  }, {
    "valueType" : "integer",
    "name" : "id",
    "value" : 23
  } ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM