fastjson依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
1.解析json字符串
@org.junit.Test
public void JsonToString() {
String jsonString = "{\n" +
" \"student\":[{\n" +
" \"name\":\"韩信\",\n" +
" \"age\":\"18\"\n" +
" },\n" +
" {\n" +
" \"name\":\"阿离\",\n" +
" \"age\":\"22\"\n" +
" },\n" +
" {\n" +
" \"name\":\"立华奏\",\n" +
" \"age\":\"21\"\n" +
" }]\n" +
"}";
JSONObject jsonObject = new JSONObject();
//将json字符串转换为jsonObject
jsonObject = jsonObject.parseObject(jsonString);
//获取json
JSONArray jsonArray = jsonObject.getJSONArray("student");
for (Object object : jsonArray) {
JSONObject object1 = (JSONObject)object;
String name = object1.getString("name");
String url = object1.getString("age");
System.out.println(name + url);
}
}
2.组装json数据
@org.junit.Test
public void stringTOJson() {
JSONObject jsonObject = new JSONObject();
//构建json数组
JSONArray jsonArray = new JSONArray();
JSONObject stObject = new JSONObject();
stObject.put("name","韩信");
stObject.put("age", "11");
JSONObject stObject1 = new JSONObject();
stObject1.put("name","阿离");
stObject1.put("age", "22");
jsonArray.add(stObject1);
jsonObject.put("student", jsonArray);
System.out.println(jsonObject.toJSONString());
}
案例:
实现如下的json拼接:
{
"code": 0,
"msg": "",
"count": 1000,
"data": [
{
"id": 110,
"saplingsId":1,
"saplingsName": 1,
"age": 1,
"baseName": 1,
"fieldsName": 1,
"coordinate": 1
}
]
}
实现代码:
package com.test.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.test.entity.Saplings;
import com.test.service.SaplingsService;
/**
* 草木管理
* @author wuhao
*
*/
@Controller
@RequestMapping("/saplings")
public class SaplingsController {
@Autowired
private SaplingsService saplingsService;
@ResponseBody
@RequestMapping("/queryforall")
public String queryforall(){
List<Saplings> saplingslist= saplingsService.queryAll();
//System.out.println(saplingslist.toString());
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
for (Saplings saplings : saplingslist) {
jsonArray.add(saplings);
}
jsonObject.put("code", 0);
jsonObject.put("code", "");
jsonObject.put("count", 1000);
jsonObject.put("data", jsonArray);
//System.out.println(jsonObject.toJSONString());
return jsonObject.toJSONString();
}
}
a) writeValueAsString(对象)
将User对象转为json字符串
ObjectMapper mapper = new ObjectMapper();
String jsonstring = mapper.writeValueAsString(User.class);
b) writeValue(参数,对象):
参数:
File: 将对象转为json字符串,保存到指定的文件中
Writer: 将对象转为json字符串,并将json数据填充到字符输出流中
outputStream: 将对象转为json字符串,并将json数据填充到字节输出流中
通过ObjectMapper类的 writeValue()方法序列化java对象至json字符串
ObjectMapper objectMapper = new ObjectMapper();
Car car = new Car("yellow", "renault");
objectMapper.writeValue(new File("target/car.json"), car);
输出:
{"color":"yellow","type":"renault"}
c) readValue
将json字符串转成User对象输出
ObjectMapper mapper = new ObjectMapper();
User user = String mapper.readValue(jsonstring,User.class);
参考链接:
https://blog.csdn.net/qq_42651904/article/details/102759877
https://blog.csdn.net/neweastsun/article/details/87940257