(转)JSON/String/Map之间的互转(fastJson/Gson/jackson的使用方法)

实例:fastjson方便一步一步分析每个json节点,实例:

void work()  {
        Teacher teacher = new Teacher();
        String s = JSON.toJSONString(teacher);
        System.out.println(s);
        Teacher teacher1 = JSON.parseObject(s, Teacher.class);
        JSONObject jsonObject = JSON.parseObject(s);
        JSONArray students = jsonObject.getJSONArray("students");
        List<Student> students1 = students.toJavaList(Student.class);
    }


package com.zjzy.tuyingdata.tuyingdata.model.dto;

import com.zjzy.tuyingdata.tuyingdata.runner.TestRunner;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class Teacher {
    private String name;
    private List<Student> students;

    public Teacher(){
        name="tttt";
        students=new ArrayList<>();
        students.add(new Student());
    }
}

package com.zjzy.tuyingdata.tuyingdata.model.dto;

import lombok.Data;

import java.util.Date;
@Data
public class Student {
    private String id;
    Student(){
        id=new Date().toString();
    }
}

https://www.cnblogs.com/ciweiyu/p/13649685.html

JSONArray封装数组对象

JSONObject封装Map对象

  • fastjson

     

    //对象-->字符串 String str = JSON.toJSONString(user); //字符串-->对象 User user_2 = JSON.parseObject(jsonData, User.class);

  • Gson

     

    //对象 --> 字符串 Gson gson = new Gson(); String str = gson.toJson(user); //字符串 --> 对象 Gson gson = new Gson(); User user_2 = gson.fromJson(jsonData, User.class);

  • jackson

     

    //对象-->String ObjectMapper objectMapper = new ObjectMapper(); String str_3 = objectMapper.writeValueAsString(user); //String-->对象 User user_3 = objectMapper.readValue(jsonData, User.class);

1|3对象集合与字符串之间的互转

  • fastjson

     

    //对象集合-->字符串 String users = JSON.toJSONString(userList); //字符串-->对象集合 List<User> userList = JSON.parseArray(userList, User.class);

  • Gson

     

    //对象集合 --> 字符串 Gson gson=new Gson(); String users=gson.toJson(list); //字符串 --> 对象集合 Gson gson = new Gson(); List<User> list = gson1.fromJson(userList, new TypeToken<List<User>>(){}.getType());

  • jackson

     

    //对象集合 --> 字符串 ObjectMapper objectMapper = new ObjectMapper(); String users_3 = objectMapper.writeValueAsString(userList); //字符串 --> 对象集合 List<User> userList_3 = objectMapper.readValue(strList, new TypeReference<List<User>>() {});

1|4字符串互转JSON对象

  • fastjson

     

    //String --> Json对象 JSONObject jsonObject = JSONObject.parseObject(jsonData); //Json --> String String jsonString = jsonObject.toJSONString();

  • Gson

     

    //String --> Json对象 JsonObject jsonObject = new JsonParser().parse(jsonData).getAsJsonObject(); //JsonObject jsonObject_2 = gson.fromJson(jsonString_2, JsonObject.class); 错误方法 返回为空(非null) //Json对象 --> String Gson gson = new Gson(); String jsonString = gson.toJson(jsonObject);

  • jackson

     

    //jackson //String --> Json对象 //暂未知,有知道的大佬请告知 //Json对象 --> String //用到fastJson格式的JSONObject ObjectMapper objectMapper = new ObjectMapper(); String jsonString_3 = objectMapper.writeValueAsString(jsonObject_1);

1|5Map与字符串之间互转

  • fastjson

     

    //字符串 --> map Map map = JSONObject.parseObject(strList, Map.class); //map --> 字符串 String jsonString = JSON.toJSONString(map);

  • Gson

     

    //字符串-->map Gson gson = new Gson(); Map map_2 = gson.fromJson(strList, Map.class); //map-->字符串 Gson gson = new Gson(); String jsonString_2 = gson.toJson(map);

  • jackson

     

    //字符串 --> map ObjectMapper objectMapper = new ObjectMapper(); Map map_3 = objectMapper.readValue(strList, Map.class); //map --> 字符串 String jsonString_3 = objectMapper.writeValueAsString(map);

1|6Map 转 JSON对象

  • fastjson

     

    //map转json对象 JSONObject json = new JSONObject(map); //json对象转Map Map map_1 = JSONObject.parseObject(strList, Map.class); //Map<String,Object> map_1 = (Map<String,Object>)jsonObject_1; 此方法也行

  • Gson

     

    //map转json对象 Gson gson = new Gson(); String jsonString_2 = gson.toJson(map); //JsonObject jsonObject_2 = gson.fromJson(jsonString_2, JsonObject.class); 错误方法 返回为空(非null) JsonObject jsonObject_2 = new JsonParser().parse(jsonString_2).getAsJsonObject(); //json对象转Map Map map_2 = gson.fromJson(strList, Map.class); System.out.println("断点");

  • jackson

     

    //map转json对象 //暂未知,有知道的大佬请告知 //json对象转Map ObjectMapper objectMapper = new ObjectMapper(); Map map_3 = objectMapper.readValue(strList, Map.class);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值