fastjson 进行json的解析和拼装和用ObjectMapper序列化和反序列化

fastjson 进行json的解析和拼装和用ObjectMapper序列化和反序列化

    • 1. 使用 fastjson 进行json的解析和拼装
    • 2. 用ObjectMapper序列化和反序列化

1. 使用 fastjson 进行json的解析和拼装

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);
        }

    }

输出结果为:
fastjson 进行json的解析和拼装和用ObjectMapper序列化和反序列化_第1张图片

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());

}

输出结果为:
fastjson 进行json的解析和拼装和用ObjectMapper序列化和反序列化_第2张图片

案例:

实现如下的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();
	}
	
}

2. 用ObjectMapper序列化和反序列化

  1. 序列化

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"}
  1. 反序列化:

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

你可能感兴趣的:(notes,json序列化,jackson)