How to deserialize JSON into flat, Map-like structure?

后端 未结 7 2132
醉梦人生
醉梦人生 2020-12-01 06:34

Have in mind that the JSON structure is not known before hand i.e. it is completely arbitrary, we only know that it is JSON format.

For example,

The followin

7条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 07:14

    How about using the json-flattener. https://github.com/wnameless/json-flattener

    BTW, I am the author of this lib.

    String flattenedJson = JsonFlattener.flatten(yourJson);
    Map flattenedJsonMap = JsonFlattener.flattenAsMap(yourJson);
    
    // Result:
    {
        "Port.@alias":"defaultHttp",
        "Port.Enabled":"true",
        "Port.Number":"10092",
        "Port.Protocol":"http",
        "Port.KeepAliveTimeout":"20000",
        "Port.ThreadPool.@enabled":"false",
        "Port.ThreadPool.Max":"150",
        "Port.ThreadPool.ThreadPriority":"5",
        "Port.ExtendedProperties.Property[0].@name":"connectionTimeout",
        "Port.ExtendedProperties.Property[0].$":"20000"
    }
    

提交回复
热议问题