java中常见的JSON操作

三世轮回 提交于 2019-12-04 06:40:40

一、什么是Json

  • Json是指JavaScript对象表示法(Java Script Object Notation)
  • Json是轻量级的文本数据交换格式
  • Json独立于语言和平台:Json解析器和Json库支持许多不同的编程语言
  • Json具有自我描述性,更易理解

二、Java中操作Json的几种方式

  • FastJson 阿里巴巴开发的 JSON 库,性能十分优秀
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.62</version>
</dependency>
  • Gson 谷歌开发的 JSON 库,功能十分全面
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.6</version>
</dependency>
  • net.sf.json 最常见,使用较多
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
<dependency>
    <groupId>net.sf.json-lib</groupId>
    <artifactId>json-lib</artifactId>
    <version>2.4</version>
	<classifier>jdk15</classifier>
</dependency>
  • Jackson 社区十分活跃且更新速度很快。
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.10.0</version>
</dependency>

1、FastJson示例

package com.powersi.test;

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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class FastJsonTest {

	/**
	 * java对象转Json
	 */
	public static String object2Json(){
		JSONObject json = new JSONObject();
		Student stu = new Student();
		stu.setId(001);
		stu.setName("张三");
		stu.setAge(18);
		json = (JSONObject) JSONObject.toJSON(stu);
		return json.toString();
	}
	
	/**
	 * json转java对象 
	 */
	public static String json2Object(){
		JSONObject json = new JSONObject();
		json.put("id", "001");
		json.put("name", "张三");
		json.put("age", "18");
		Student s = JSONObject.toJavaObject(json, Student.class);
		return s.toString();
	}
	
	/**
	 * json转字符串
	 */
	public static String json2String(){
		JSONObject json = new JSONObject();
		json.put("id", "001");
		json.put("name", "张三");
		json.put("age", "18");
		String s = JSONObject.toJSONString(json);
		return s;
	}
	
	/**
	 * 字符串转json
	 */
	public static String string2Json(){
		JSONObject json = new JSONObject();
		String s = "{\"id\":\"001\", \"name\":\"张三\", \"age\":\"18\"}";
		json = JSONObject.parseObject(s);
	    return json.toString();
	}
	
	/**
	 * 集合转json数组
	 */
	public static String list2JsonArray(){
		Student stu = new Student();
		stu.setId(001);
		stu.setName("张三");
		stu.setAge(18);
		
		Student stu2 = new Student();
		stu2.setId(002);
		stu2.setName("李四");
		stu2.setAge(19);
		
		List<Student> stuList = new ArrayList<Student>();
		stuList.add(stu);
		stuList.add(stu2);
		
		String s = JSONArray.toJSONString(stuList);
		return s;
	}
	
	public static void main(String[] args) {
		System.out.println(FastJsonTest.list2JsonArray());
	}
}

2、Gson示例

package com.powersi.test;

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

import com.google.gson.Gson;

public class GsonTest {

	/**
	 * java对象转Json
	 */
	public static String object2Json(){
		Gson gson = new Gson();
		Student stu = new Student();
		stu.setId(001);
		stu.setName("张三");
		stu.setAge(18);
		String s = gson.toJson(stu);
		return s;
	}
	
	/**
	 * json转java对象 
	 */
	public static String json2Object(){
		Gson gson = new Gson();
		String jsonStr = object2Json();
		Student s = gson.fromJson(jsonStr, Student.class);
		return s.toString();
	}
	
	
	/**
	 * 对象转json数组
	 */
	public static String list2JsonArray(){
		Gson gson = new Gson();
		
		Student stu = new Student();
		stu.setId(001);
		stu.setName("张三");
		stu.setAge(18);
		
		Student stu2 = new Student();
		stu2.setId(002);
		stu2.setName("李四");
		stu2.setAge(19);
		
		List<Student> stuList = new ArrayList<Student>();
		stuList.add(stu);
		stuList.add(stu2);
		
		String s = gson.toJson(stuList);
		return s;
	}
	
	public static void main(String[] args) {
		System.out.println(GsonTest.list2JsonArray());
	}
}

3、net.sf.json示例

package com.powersi.test;

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

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class NetSFJsonTest {

	/**
	 * java对象转Json
	 */
	public static String object2Json(){
		JSONObject json = new JSONObject();
		Student stu = new Student();
		stu.setId(001);
		stu.setName("张三");
		stu.setAge(18);
		json = JSONObject.fromObject(stu);
		return json.toString();
	}
	
	/**
	 * json转java对象 
	 */
	public static String json2Object(){
		String jsonStr = object2Json();
		Student stu = (Student)JSONObject.toBean(JSONObject.fromObject(jsonStr), Student.class);
		return stu.toString();
	}
	
	
	/**
	 * 对象转json数组
	 */
	public static String list2JsonArray(){
		
		Student stu = new Student();
		stu.setId(001);
		stu.setName("张三");
		stu.setAge(18);
		
		Student stu2 = new Student();
		stu2.setId(002);
		stu2.setName("李四");
		stu2.setAge(19);
		
		List<Student> stuList = new ArrayList<Student>();
		stuList.add(stu);
		stuList.add(stu2);
		
		JSONArray jsonArray = JSONArray.fromObject(stuList);
		return jsonArray.toString();
	}
	
	public static void main(String[] args) {
		System.out.println(NetSFJsonTest.list2JsonArray());
	}
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!