GSON有几个已知的类 [英] GSON with several known classes

查看:73
本文介绍了GSON有几个已知的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下json

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

我知道我可以做点什么像

  class file {
public String file
public String owner
}
类方法{
public String name
public String value
}



<我可以调用

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

但是如果我需要处理一个复杂的包含许多对象的复杂json,那么该怎么办?
我无法指定 gson.fromJson(jsonInString,ListOfClasses)

解决方案

我通常遵循这种方法来获取从JSON转换为对象的任何复杂类。这种方法适用于几乎所有的东西,如列表,地图等。这个想法很简单,为复杂的类创建持有者,然后创建类。尽可能多的深度要求。诀窍是在Json和你的持有者(和子类)中匹配名字。



文件配置:

  class FileConfig {
public String file;
公共字符串所有者;

//定义toString,getters和setters
}

方法类:

 类方法{
public String name;
public String value;

//定义toString,getters和setters
}

方法配置:

  class MethodConfig {
List< Method> methods = null;

//定义toString,getters和setters
}

持有配置:

  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;


$ / code $ / pre

构建配置:

  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;


演示课程:

  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(fileConfig is:+ file.toString());


输入Json:

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

注意:编写代码以在测试代码中获取输入JSON。

通过这种方式,无论何时添加更多元素到您的JSON中,您都必须为该元素创建一个单独的类,并将元素名称与您的json中的相同添加到HolderConfig中。你不需要改变其余的代码。



希望它有帮助。


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)

解决方案

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.

这篇关于GSON有几个已知的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆