java - 将JSON映射到Java对象

原文 标签 java json gson

我有从Gridx过滤器表达式中得到的这些类型的JSON对象:

{
  "op": "or",
  "data": [
    {
      "op": "contain",
      "data": [
        {
          "op": "string",
          "data": "id",
          "isCol": true
        },
        {
          "op": "string",
          "data": "sdfv"
        }
      ]
    },      
    {
      "op": "contain",
      "data": [
        {
          "op": "string",
          "data": "post",
          "isCol": true
        },
        {
          "op": "string",
          "data": "sdfv"
        }
      ]
    },
    {
      "op": "contain",
      "data": [
        {
          "op": "string",
          "data": "birthday",
          "isCol": true
        },
        {
          "op": "string",
          "data": "sdfv"
        }
      ]
    }
  ]
}


如何将其映射到Java对象,然后使用Gson反序列化?

我完成了这两节课:

package dto.Filter;

public class FilterData extends FilterExpression {
    private String op;
    private boolean isCol;
    private String data;

    public String getOp() {
        return op;
    }

    public void setOp(String op) {
        this.op = op;
    }

    public boolean isCol() {
        return isCol;
    }

    public void setCol(boolean col) {
        isCol = col;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}

package dto.Filter;

import java.util.List;

public class FilterExpression {
    private List<FilterData> filters;
    private String op;

    public List<FilterData> getFilters() {
        return filters;
    }

    public void setFilters(List<FilterData> filters) {
        this.filters = filters;
    }

    public String getOp() {
        return op;
    }

    public void setOp(String op) {
        this.op = op;
    }
}


问题是我同时具有ObjectString类型的数据。我需要使用自定义的TypeAdapter吗?

最佳答案

使您的数据结构为

class DataStructure {

    private String op;
    private String data;
    private String isCol;

    public DataStructure(){

        op="";
        data="";
        isCol="";
    }

    public String getOp() {
        return op;
    }

    public void setOp(String op) {
        this.op = op;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
         this.data = data;
    }

    public String isCol() {
        return isCol;
    }

    public void setCol(String isCol) {
        this.isCol = isCol;
    }

    @Override
    public String toString() {
        return "DataStructure [op=" + op + ", data=" + data + ", isCol=" + isCol + "]";
    }

}


我已经使用Google GSON库解析了JSON文件。这是maven repository

注意:要使用GSON库阅读,请在开头添加'[',在结尾添加']'。

我已经阅读了JSON文件并将数据存储在ArrayList中。希望在获取数组列表后可以进行序列化。

public class FilterData {

    private static Gson gson = new Gson();
    private static JsonParser parser = new JsonParser();

    public static List<DataStructure> getData(List<DataStructure> datas){

        List<DataStructure> result = new ArrayList<>();

        for (DataStructure data : datas) {

            for (JsonElement object : parser.parse(data.getData()).getAsJsonArray()) {

                DataStructure dataStructure = new DataStructure();

                JsonObject jObject = gson.fromJson(object, JsonObject.class);

                dataStructure.setOp(jObject.get("op").toString());
                dataStructure.setData(jObject.get("data").toString());

                if (jObject.has("isCol"))
                    dataStructure.setData(jObject.get("isCol").toString());

               System.out.println(dataStructure);
               result.add(dataStructure);
            }
        }

        return result;

    }

    public static void main(String[] args) throws FileNotFoundException {

        JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream("input.json")));

        List<DataStructure> datas = new ArrayList<>();

        JsonArray jArray = parser.parse(reader).getAsJsonArray();

        for (JsonElement object : jArray) {

            DataStructure dataStructure = new DataStructure();

            JsonObject jObject = gson.fromJson(object, JsonObject.class);

            dataStructure.setOp(jObject.get("op").toString());
            dataStructure.setData(jObject.get("data").toString());

             if (jObject.has("isCol"))
                 dataStructure.setData(jObject.get("isCol").toString());

            System.out.println(dataStructure);
            datas.add(dataStructure);
        }

        List<DataStructure> insideData = getData(datas);
        List<DataStructure> inside2Data = getData(insideData);
    }
}

相关文章:

java - 如何使用Gson反序列化子类?

java - Android Nullbale和NonNull:方法调用“ getSearchableInfo”可能会产生“ NullPointerException”

jquery - jQuery排序JSON对象

java - 尝试获取Json中某个元素内的元素或其自身的值将返回null

java - 使用Java在运行时确定通用类型参数

java - JSON对象GSON进入列表

java - 此代码有什么问题? (java)(错误:找不到符号方法getValue())

java - Jar文件在签名之前都可以很好地工作(在Windows中)

java - 使用JFXPanel为本机应用程序构建Facebook OAuth登录流程

java - 如何解析android volley gson请求到列表/对象集合