序列化与反序列化

1 KV结构且K不确定

Example:

  public static void main(String[] args) {
        String response =
                "{\n" +
                        "  \"rule\":[\n" +
                        "        {\n" +
                        "            \"level\":\"info\",\n" +
                        "            \"on\": true,\n" +
                        "            \"ipList\":[],\n" +
                        "            \"router\":[\n" +
                        "              {\n" +
                        "                \"type\": \"default_type\",\n" +
                        "                \"id\":\"12312\"\n" +
                        "              }\n" +
                        "            ]\n" +
                        "        },\n" +
                        "        {\n" +
                        "            \"level\":\"error\",\n" +
                        "            \"on\": false,\n" +
                        "            \"ipList\":[\"127.0.0.1\"],\n" +
                        "            \"router\":[]\n" +
                        "        }\n" +
                        "    ]\n" +
                        "}\n";
        JSONObject dataJson = JSONObject.parseObject(response);
        Map<String, Object> map = dataJson.getInnerMap();
        for (String key : map.keySet()) {
            List value = (List) map.get(key);
            System.out.println("key:" + key);
            for (Object object : value) {
                // 将字符串变为对象
                Gson gson = new Gson();
                LoggerSwitch loggerSwitch = gson.fromJson(object.toString(), LoggerSwitch.class);
                System.out.println("level:" + loggerSwitch.getLevel());
                System.out.println("on:" + loggerSwitch.getOn());
                System.out.println("ipList:" + loggerSwitch.getIpList());
                System.out.println("router:" + loggerSwitch.getRouter());
            }
        }
    }

依赖:

<dependency>
  <groupId>com.google.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.2.4</version>
</dependency>

2 KV结构K确定

Example:

// 反序列化
public static void main(String[]args){
   BaseResult<ServiceUrl> baseResult = getBaseResult(responseBody, new TypeReference<BaseResult<ServiceUrl>>() {});
}

/**
* 反序列化
*/
public static <T> T getBaseResult(String responseBody, TypeReference<T> typeReference) {
   T t = null;
   try {
      t = JSONArray.parseObject(responseBody, typeReference);
   } catch (Exception e) {
  }
  return t;
}

/**
* BaseResult
*/
@Data
public class BaseResult<T> implements Serializable {

    protected String resultCode;
    protected String resultMsg;
    protected T resultData;
    protected T data;
    protected String code;
    protected String msg;
    protected String ip;

    public BaseResult() {
    }
}

public class ServiceUrl implements Serializable {
    private String asapiUrl;
    private String dtUrl;
    private List<String> tianjiUrl;
    private String asoUrl;
    private String ascmUrl;
}

依赖:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.18</version>
</dependency>

 

posted @ 2020-05-02 10:43  FCity  阅读(482)  评论(0编辑  收藏  举报