java json

Google Gson用法详解 - 云+社区 - 腾讯云 (tencent.com)
<dependency>
 <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.8.5</version>
</dependency>

阿里巴巴json

(41条消息) Json详解以及fastjson使用教程_名字真难娶的博客-CSDN博客_fastjson

<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.66</version>
</dependency>

 
public class Student {
	
	private String name;
	private int age;
	
	public Student() {
		// TODO Auto-generated constructor stub
	}
	
	public Student(String name,int age){
		this.name=name;
		this.age=age;
	}
	
	public void setName(String name){
		this.name=name;
		
	}
	
	public String getName(){
		return name;
	}
	
	public void setAge(int age){
		this.age=age;
	}
	
	public int getAge(){
		return age;
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "student [name="+name+" , "+"age="+age+"]";
	}
	
}

 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
 
 
public class MyJson {
	
	public static void main(String[] args) {
		
		List<Student> list=new ArrayList<>();
		Student student=new Student("bob",24);
		Student student12=new Student("lily", 23);
		list.add(student);
		list.add(student12);
		System.out.println("*******javaBean  to jsonString*******");
		String str1=JSON.toJSONString(student);
		System.out.println(str1);
		System.out.println(JSON.toJSONString(list));
		System.out.println();
		
		System.out.println("******jsonString to javaBean*******");
		//Student stu1=JSON.parseObject(str1,new TypeReference<Student>(){});
		Student stu1=JSON.parseObject(str1,Student.class);
		System.out.println(stu1);
		System.out.println();
		
		System.out.println("******javaBean to jsonObject******");
		JSONObject jsonObject1=(JSONObject)JSON.toJSON(student);
		System.out.println(jsonObject1.getString("name"));
		System.out.println();
		
		System.out.println("******jsonObject to javaBean******");
		Student student2=JSON.toJavaObject(jsonObject1, Student.class);
		System.out.println(student2);
		System.out.println();
		
		System.out.println("*******javaBean to jsonArray******");
		List<Student> stulist=new ArrayList<>();
		for(int i=0;i<5;i++){
			stulist.add(new Student("student"+i, i));
			
		}
		JSONArray jsonArrays=(JSONArray)JSON.toJSON(stulist);
		for(int i=0;i<jsonArrays.size();i++){
		System.out.println(jsonArrays.getJSONObject(i));
		}
		System.out.println();
		
		System.out.println("*****jsonArry to javalist******");
		List<Student> myList=new ArrayList<>();
		for(int i=0;i<jsonArrays.size();i++){
			
		Student student3=JSON.toJavaObject(jsonArrays.getJSONObject(i), Student.class);
			myList.add(student3);
		}
		for(Student stu:myList){
			System.out.println(stu);
		}
		
	        System.out.println();
		
		System.out.println("*****jsonObject to jsonString*****");
		String str4=JSON.toJSONString(jsonObject1);
		System.out.println(str4);
		System.out.println();
		
		System.out.println("*******jsonString to jsonObject*****");
		JSONObject jso1=JSON.parseObject(str1);
		System.out.println(jso1.getString("name"));
		System.out.println();
		
		System.out.println("*****jsonString to jsonArray*****");
		JSONArray jArray=JSON.parseArray(JSON.toJSONString(stulist));
		for(int i=0;i<jArray.size();i++){
		System.out.println(jArray.getJSONObject(i));
		}
		System.out.println();
	}
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值