How to deserialize json string into object

后端 未结 4 2035
心在旅途
心在旅途 2021-01-23 06:07
{
   \"LocalLocationId [id=1]\":{
      \"type\":\"folderlocation\",
      \"id\":{
         \"type\":\"locallocationid\",
         \"id\":1
      },
      \"parentId\":         


        
4条回答
  •  借酒劲吻你
    2021-01-23 07:01

    @Kedar

    I'll assume you are in control of how the JSON input string is created. I think the JSON string is not formatted correctly for default GSON deserialization of Map types.

    I have modified the input string for your consideration and this results in a non null LocalLocationId

    {
       "LocalLocationId":[
       [
         "1",
           {
              "type":"folderlocation",
              "id":{
                 "type":"locallocationid",
                 "id":1
              },
              "parentId":{
                 "type":"locallocationid",
                 "id":0
              },
              "name":"Test",
              "accessibleToUser":true,
              "defaultLocation":false,
              "timezoneId":"Asia/Calcutta",
              "children":[]
           }
       ],
       [
         "2",
           {
              "type":"folderlocation",
              "id":{
                 "type":"locallocationid",
                 "id":0
              },
              "parentId":null,
              "name":"Locations",
              "accessibleToUser":false,
              "defaultLocation":false,
              "timezoneId":"Asia/Calcutta",
              "children":[{
                 "type":"locallocationid",
                 "id":1
              }]
           }
       ]
       ],
       "allAllowedChildren":[{
          "type":"locallocationid",
          "id":1
       }]
    }
    

    Please comment if my assumption about the input string is incorrect.

    EDIT 1: Since input cannot be modified, consider writing custom Deserializer. Below is the way to register custom deserialisation class

    GsonBuilder gsonb = new GsonBuilder();
            gsonb.registerTypeAdapter(Tree.class, new TreeDeserializer());
            Gson gson = gsonb.create();
    

    Below is the TreeDeserializer

    public class TreeDeserializer implements JsonDeserializer {
    
        public Tree deserialize(JsonElement json, Type typeOfT,
                JsonDeserializationContext context) throws JsonParseException {
            Tree out = new Tree();
    
            if (json != null) {
                JsonObject obj  = json.getAsJsonObject();
                Set> entries = obj.entrySet();
                for (Map.Entry e: entries) {
                    if (e.getKey().equals("allAllowedChildren")) {
                        Type ft = List.class;
                        System.out.println(context.deserialize(e.getValue(), ft));
                        // TODO add this back into the Tree out object
                    } else {
                        // LocalLocationId
                        System.out.println(e.getKey());
                        System.out.println(context.deserialize(e.getValue(), Tree.LocalLocationId.class));
    
                        // TODO add this back into the Tree out object
                    }
                }
            } 
            return out;
        }
    
    }
    

    Here is the console output from the Sysouts.

    LocalLocationId [id=1]
    org.test.StackOverflowAnswers.Tree$LocalLocationId@464bee09
    LocalLocationId [id=0]
    org.test.StackOverflowAnswers.Tree$LocalLocationId@f6c48ac
    [{type=locallocationid, id=1.0}]
    org.test.StackOverflowAnswers.Tree@589838eb
    

    I have left TODOs in the deserialiser where you'll need to write custom code to inject the values from deserialisation into the Tree class just created. Hope this helps. Can't provide full implementation, but I think this would be a partial solution

提交回复
热议问题