fastjson对象,JSON,字符串,map之间的互相转换

1.对象与字符串之间的互转

//将对象转换成为字符串
String str = JSON.toJSONString(infoDo);
//字符串转换成为对象
InfoDo infoDo = JSON.parseObject(strInfoDo, InfoDo.class);

2.对象集合与字符串之间的互转

//将对象集合转换成为字符串
String users = JSON.toJSONString(users);

//将json字符串转成对象
User user= JSON.parseObject(users,User.class);

//map转对象
User user= JSON.parseObject(JSONObject.toJSONString(map),User.class);



// 将对象转成JsonObject

JSONObject jsonStu = (JSONObject) JSONObject.toJSON(user);  

//将字符串转换成为对象集合
List<User> userList = JSON.parseArray(userStr, User.class);  

	

3.字符串互转JSONObject

//String 转 Json对象
JSONObject jsonObject = JSONObject.parseObject(jsonString);
//String 转 JSONArray 对象
 JSONArray jsonObject3= JSONArray.parseArray(jsonString);


//json对象转string
JSONObject jsonObject = JSONObject.parseObject(str);//json对象转字符串 
String jsonString = jsonObject.toJSONString();

4.map与字符串之间互转

  //字符串转map
  JSONObject  jsonObject = JSONObject.parseObject(str);
  Map<String,Object> map = (Map<String,Object>)jsonObject;//    //json对象转Map
  //map转字符串
  String jsonString = JSON.toJSONString(map);

5.Map 转 Json对象互转

   //map转json对象
    Map<String,Object> map = new HashMap<>();
    map.put("age", 24);
    map.put("name", "cool_summer_moon");
    JSONObject json = new JSONObject(map);
  //json对象转Map 
  Map<String,Object> map = (Map<String,Object>)jsonObject; 

6.创建json对象

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("name", "Jason");
    jsonObject.put("id", 1);
    jsonObject.put("phone", "18271415782");
    System.out.println(jsonObject.toString());

7.List和JSONArray互转

//1.fastjson  List转JSONArray
List<T> list = new ArrayList<T>();
JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));


//2.fastjson  JSONArray转List
JSONArray array = new JSONArray();
List<EventColAttr> list = JSONObject.parseArray(array.toJSONString(), EventColAttr.class);


//3.fastjson  字符串转List
String str = "";
List<T> list = JSONObject.parseArray(str,T.class);

8.判断是否存在key

jsonObject.containsKey("key")

9.遍历JSONObject /map

JSONObject response = JSONObject.parseObject(result);
			for (Map.Entry<String, Object> entry : response.entrySet()) {
				System.out.println(entry.getKey() + ":" + entry.getValue());
			}

// 传统的Map迭代方式
for (Map.Entry<String, Object> entry : map.entrySet()) {
    System.out.println(entry.getKey() + ":" + entry.getValue());
}
// JDK8的迭代方式
map.forEach((key, value) -> {
    System.out.println(key + ":" + value);
});

10.Gson

//解成对象
Fromat mFromat = new Gson().fromJson(jsonStringObject, Fromat.class);

//解成对象组
LinkedList<Fromat> list = new LinkedList<MainActivity.Fromat>();
Type type = new TypeToken<LinkedList<Fromat>>(){}.getType();
list = new Gson().fromJson(jsonStringArray, type);

//泛型统一封装时 需要传个 type 进来
new TypeToken<LinkedList<Fromat>>(){}.getType();
fromJson(String json, Type typeOfT)
public <T> T fromJson(String json, Type typeOfT)
//解成对象
Fromat mFromat = new Gson().fromJson(jsonStringObject, Fromat.class);

//解成对象组
LinkedList<Fromat> list = new LinkedList<MainActivity.Fromat>();
Type type = new TypeToken<LinkedList<Fromat>>(){}.getType();
list = new Gson().fromJson(jsonStringArray, type);

//泛型统一封装时 需要传个 type 进来
new TypeToken<LinkedList<Fromat>>(){}.getType();
fromJson(String json, Type typeOfT)
public <T> T fromJson(String json, Type typeOfT)

//json转map

  Gson gson = new Gson();
  Map<String,String> map = new HashMap<>();
  JSONObject strJson = JSONObject.fromObject(data);
  map= gson.fromJson(strJson.toString(), map.getClass());

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值