GSON with several known classes

谁都会走 提交于 2019-12-12 15:17:13

问题


I have the following json

{ "file": {"file": "foo.c", "owner": "user123"}
  "methods": [{"name": "proc1", "value":"val"}, {"name":"proc2","value":"val2"}]
  etc...
}

I know that I can do something like

class file{
      public String file
      public String owner
    }
class methods{
      public String name
      public String value
    }

and I can either call

File file= gson.fromJson(jsonInString, File.class);  
methods[] array = gson.fromJson(jsonInString, methods[].class);

but what do I do if I need to handle a complex json that contains many objects all togther I cannot specify gson.fromJson(jsonInString, ListOfClasses)


回答1:


I normally follow this approach to get any complex classes converted from json to object. This approach works for almost everything like list, map etc. The idea is simple create holders for the complex classes and then create the classes. Give as much depth as much required. The trick is to match name in Json and your holders (and subclasses).

File Config:

class FileConfig{
  public String file;
  public String owner;

  //define toString, getters and setters 
}

Method Class:

class Method{
  public String name;
  public String value;

  //define toString, getters and setters   
}

Method Config:

class MethodConfig{
  List<Method> methods = null;

  //define toString, getters and setters 
}

Holding the Config:

public class HolderConfig {

  private FileConfig file = null;
  private MethodConfig methods = null;

  public FileConfig getFile() {
    return file;
  }

  public void setFile(FileConfig file) {
    this.file = file;
  }
  public MethodConfig getMethods() {
    return file;
  }

  public void setMethods(MethodConfig methods) {
    this.methods = methods;
  } 
}

Building the config:

public class HolderConfigBuilder {

public static HolderConfig build(JsonObject holderConfigJson) {

    HolderConfig configHolderInstance = null;         

    Gson gsonInstance = null;

    gsonInstance = new GsonBuilder().create();

    configHolderInstance = gsonInstance.fromJson(holderConfigJson,HolderConfig.class);

    return configHolderInstance;
  }
}

Demo class:

public class App 
{
      public static void main( String[] args )
      {

           HolderConfig configHolderInstance = null;
           FileConfig file = null;
           configHolderInstance = HolderConfigBuilder.build(<Input Json>);
           file = configHolderInstance.getFile();
           System.out.println("The fileConfig is : "+file.toString());
      }
 }

Input Json:

{ "file": {"file": "foo.c", "owner": "user123"}
  "methods": [
               {"name": "proc1", "value":"val"},
               {"name":"proc2","value":"val2"}
             ]
}

Note: Write the code to get Input JSON in your test code.

In this way whenever you add more elements to your JSON you have to create a separate class for that element and just add the element name same as in your json into the HolderConfig. You need not change rest of the code.

Hope it helps.



来源:https://stackoverflow.com/questions/38226024/gson-with-several-known-classes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!