JSON数据的拼接与解析
首先使用阿里的fastjson里的一个方法,导入fastjson包
在IDEA的pom文件中加入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
常用方法API
1. 将对象序列化成json字符串
String com.alibaba.fastjson.JSON.toJSONString(Object object)
2. 将json字符串反序列化成对象
<T> Project com.alibaba.fastjson.JSON.parseObject(String text, Class<T> clazz)
3. 将json字符串反序列化成JSON对象
JSONObject com.alibaba.fastjson.JSON.parseObject(String text)
4.根据key 得到json中的json数组
JSONArray com.alibaba.fastjson.JSONObject.getJSONArray(String key)
5. 根据下标拿到json数组的json对象
JSONObject com.alibaba.fastjson.JSONArray.getJSONObject(int index)
6.. 根据key拿到json的字符串值
String com.alibaba.fastjson.JSONObject.getString(String key)
7. 根据key拿到json的int值
int com.alibaba.fastjson.JSONObject.getIntValue(String key)
8. 根据key拿到json的boolean值
boolean com.alibaba.fastjson.JSONObject.getBooleanValue(String key)
实战练习
创建对象 ResponseObject
package com.srit.sxmarket.pojo.common;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseObject<T> {
private Integer code;
private String msg;
private T data;
public ResponseObject(Integer code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
}
实验几个方法
package com.srit.sxmarket.util.shiyan;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.srit.sxmarket.pojo.common.ResponseObject;
import java.util.*;
import java.util.stream.Collectors;
public class Test {
public static void main(String args[]){
mapLianxi();
}
public static void mapLianxi(){
Map<String,Object> map=new HashMap<>();
map.put("name","啦啦啦");
map.put("sex","男");
map.put("age","23");
List listNew=new ArrayList();
listNew.add(map);
Map<String,Object> map2=new HashMap<>();
map2.put("name","嘿嘿嘿");
map2.put("sex","男");
map2.put("age","23");
listNew.add(map2);
Map<String,Object> map3=new HashMap<>();
map3.put("people",listNew);
ResponseObject responseObject=new ResponseObject(1,"啦啦啦啦",map3);
//传过来的json
String json=JSONObject.toJSON(responseObject).toString();
System.out.println("形成的json字符串内容为:"+json);
//转成json对象
JSONObject jsonObject=JSONObject.parseObject(json);
//获取json的code和msg
System.out.println("code "+jsonObject.getString("code")+" msg "+jsonObject.getString("msg"));
//获取json的data对象内的对象信息
JSONArray jsonArray=jsonObject.getJSONObject("data").getJSONArray("people");
if(jsonArray.size()>0){
for (int i=0;i<jsonArray.size();i++){
System.out.println(jsonArray.get(i).toString());
}
}
}
}
生成的json格式为
将对象拼接为Json格式代码:
ResponseObject responseObject=new ResponseObject(1,"啦啦啦啦",map3);
//传过来的json
String json=JSONObject.toJSON(responseObject).toString();
System.out.println("形成的json字符串内容为:"+json);
转成将Json字符串解析成对象代码:
//转成json对象
JSONObject jsonObject=JSONObject.parseObject(json);
获取Json字符串中code,msg,people中内容代码:
//获取json的code和msg
System.out.println("code "+jsonObject.getString("code")+" msg "+jsonObject.getString("msg"));
//获取json的data对象内的对象信息
JSONArray jsonArray=jsonObject.getJSONObject("data").getJSONArray("people");
if(jsonArray.size()>0){
for (int i=0;i<jsonArray.size();i++){
System.out.println(jsonArray.get(i).toString());
}
}
例子2
HashMap<String,String>map =
new
HashMap<>();
map.put(
"a"
,
"1"
);
map.put(
"b"
,
"2"
);
map.put(
"c"
,
"3"
);
String json = JSON.toJSONString(map);
//map转String
JSONObject jsonObject = JSON.parseObject(json);
//String转jso
//json转map
Map<String, String> jsonMap = JSONObject.toJavaObject(jsonObject, Map.
class
);
//String转map
Map<String, String> jsonMap1 = JSONObject.parseObject(json, Map.
class
);
想了解更多详细也可参考此链接文章 https://segmentfault.com/a/1190000015363286