u012536675
whueuwe
采纳率9.1%
2019-01-28 16:47

json字符串反序列化成实体类,新手求解

5

现在有json字符串
{
“header”:{
“state”:”success”,
“count”:”视频个数”
},
“body”:[“http://10.10.10.10/123.mp4”,”http://10.10.10.10/234.mp4”]
}

怎么建实体类,并且反序列化

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • sheng94458 sheng94458 2年前

    这个不是很简单么?

    第一个实体:
    @Data
    public class ResponseDemo implements Serializable {

    private static final long serialVersionUID = -6864708554168505352L;
    
    private ResponseHeaderDemo header;
    
    private List<String> body;
    

    }

    第二个实体

    @Data
    public class ResponseHeaderDemo implements Serializable {

    private static final long serialVersionUID = -1838521376200675071L;
    private boolean state;
    
    private String count;
    
    
    public ResponseHeaderDemo() {
    }
    
    public ResponseHeaderDemo(boolean state, String count) {
        this.state = state;
        this.count = count;
    }
    

    }

    测试类:

    public class TestObjSerializeAndDeserialize {

    public static void main(String[] args) throws Exception {
        //序列化Person对象
        SerializeResponseDemo();
    
        //反序列Perons对象
        ResponseDemo responseDemo = DeserializePerson();
        System.out.println("解析出来的对象" + new ObjectMapper().writeValueAsString(responseDemo));
    }
    
    /**
     * MethodName: SerializePerson
     * Description: 序列化Person对象
     * @author xudp
     * @throws FileNotFoundException
     * @throws IOException
     */
    private static void SerializeResponseDemo() {
        ObjectOutputStream oo = null;
        try {
            ResponseDemo responseDemo = new ResponseDemo();
            responseDemo.setBody(Arrays.asList("http://10.10.10.10/123.mp4", "http://10.10.10.10/123.mp4"));
            responseDemo.setHeader(new ResponseHeaderDemo(false, "视频个数"));
            // ObjectOutputStream 对象输出流,将ResponseDemo对象存储到E盘的ResponseDemo.txt文件中,完成对Person对象的序列化操作
            oo = new ObjectOutputStream(new FileOutputStream(new File("E:/ResponseDemo.txt")));
            oo.writeObject(responseDemo);
            System.out.println("ResponseDemo对象序列化成功!");
    
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                oo.close();
            } catch (IOException e) {
                System.out.println("关闭对象失败!");
                e.printStackTrace();
            }
        }
    }
    
    /**
     * MethodName: DeserializePerson
     * Description: 反序列Perons对象
     * @author xudp
     * @return
     * @throws Exception
     * @throws IOException
     */
    private static ResponseDemo DeserializePerson() throws Exception, IOException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
                new File("E:/ResponseDemo.txt")));
        ResponseDemo person = (ResponseDemo) ois.readObject();
        System.out.println("ResponseDemo对象反序列化成功!");
        return person;
    }
    

    }

    点赞 1 评论 复制链接分享
  • floatwf floatwf 2年前

    如果不一定要一个实体类,就先用一个map接收,也可以取出数据啊

    点赞 评论 复制链接分享
  • qq_39181568 MY_MAIN 2年前

    啥语言啊???!!!

    下面是java fastJson

    https://www.cnblogs.com/cdf-opensource-007/p/7106018.html

    点赞 评论 复制链接分享

相关推荐