首页 > 编程知识 正文

getrawtype,fastjson将list转换成json

时间:2023-05-05 12:59:17 阅读:146007 作者:4867

我以前不知道,在谷粒商业街确实学过。 于是我去了github,知道了它的使用方法

1 .基本使用fastjson提供了类TypeReference来处理通用的反序列化。

import com.Alibaba.fast JSON.type reference; ListVO list=JSON.parseObject (' . ',new TypeReferenceListVO ) ) }; 写如下,会有更好的性能

import com.Alibaba.fast JSON.type reference; finalstatictypetype=newtypereferencelistvo ({ }.gettype ); ListVO list=JSON.parseObject (' . ',type ); 在这个例子中,通过TypeReference可以解决List中的t的类型问题。

2 .在使用带参数的1.2.9 1.1.49.android版本中,TypeReference支持通用参数,可以在某些框架中实现通用的反序列化类。 使用方法如下。

2.1 .单参数示例publicclassresponset { public tdata; } publicstatictresponsetparsetomap (字符串JSON,ClassT type ) return JSON.parse object (JSON,newtypereferenceresponset )。 } 2.2 .双重参数示例公共静态k、V MapK、vparsetomap(stringJSON,ClassK keyType, ClassV valueType ) returnJSON.parseobject ) jas vvalue//string JSON=' { 1: } name :' DDD',} MapInteger,modelmap=parsetomap(JSON,Integer.class,Model.class ); assertequals('DDD ',map.get(1) )1).name ); assertequals('zzz ',map.get(2) )2).name;

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。