码迷,mamicode.com
首页 > 其他好文 > 详细

序列化与反序列化

时间:2020-05-02 11:49:45      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:tle   result   序列   sele   turn   序列化   serial   body   ati   

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>

 

序列化与反序列化

标签:tle   result   序列   sele   turn   序列化   serial   body   ati   

原文地址:https://www.cnblogs.com/huaiheng/p/12817266.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!