java 对json的处理,主流json jar 包转换方式

3 篇文章 0 订阅

主流Java json 库的实现分析: https://blog.csdn.net/u013063153/article/details/73163624
 

1.fastjson

主要处理对象是JSON,通过JSON.toJSONString(obj) 来将数据转为json 格式,通过 JSON.parseObject 将数据转换为所需格式

其他类型转为json 类型的String
 String json = JSON.toJSONString(model); // serializes model to Json
 
 jsonString 转其他类型
 Model model2 = JSON.parseObject(json, Model.class); // deserializes json into model2
 
 其他类型转jsonObject
JSONObject jb = JSON.parseObject(String text);

2.gson

主要处理对象是Gson ,通过Gson.toJson(obj) 转为json 格式 ,通过Gson.fromJson 转换为所需的java对象

String 类型转 gson, gson 转其他类型
        Gson gson = new Gson();
        String json = gson.toJson(str);
        System.out.println(json);
        String fromJson = gson.fromJson(json, String.class);
        System.out.println(fromJson);
        json 转 map
        Map<String,Object> map = new Gson().fromJson(urlGetToQualifiedPath, new TypeToken<HashMap<String,Object>>(){}.getType());//gson

3.json-lib

JSONObject fromObject = JSONObject.fromObject(JsonMessage.str);
        System.out.println(fromObject);
        String bean = (String) JSONObject.toBean(fromObject, String.class);
        System.out.println(bean);

4.jackson

主要是ObjectMapper对象
        json 转  map
        String responseMessageGet = TestOKHttp.getResponseMessageGet(url);
         ObjectMapper mapper = new ObjectMapper();
         Map<String,String> reMap = null;
        reMap = mapper.readValue(responseMessageGet, new TypeReference<Map<String,Object>>(){});
        map 转 json
         ObjectMapper mapper = new ObjectMapper();
           String jsonReturn = null;
        jsonReturn = mapper.writeValueAsString(map);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值