Json数据对象——java操作

Json是一种轻量级的用于数据传输语言。

基本格式:

{key:value}

源码与jar包已上传github上

       1、java对象 => json 对象 => String(json)       

       2、java List=>json 数组对象=>String(json)

       3、String(json)=>json对象=>java对象

       4、String(json)=> json对象=>java 对象数组

//BootMain.java

package com.cc.yonyou.funOne;

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

import com.cc.yonyou.People;

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

public class BootMain {
	
	public static void main(String[] args){
		String[] school = {"小学","初中","高中"};
		People p = new People();
		p.setName("Tom");
		p.setAge(20);
		p.setSchool(school);
		System.out.println("--------------json对象-----------");
		JSONObject jsonObj = JSONObject.fromObject(p);
		System.out.println(jsonObj.toString());
		
		System.out.println("--------------json数组-----------");

		People p1 = new People();
		p1.setName("Jack");
		p1.setAge(22);
		p1.setSchool(school);
		List list = new ArrayList();
		list.add(p);
		list.add(p1);
		//2、json数组
		JSONArray jsonListAry = JSONArray.fromObject(list);
		System.out.println(jsonListAry.toString());
		
		
		//字符串转对象
		String objStr = "{'school':['小学','初中','高中'],'name':'Tom','age':20}";
		String objList = "[{'school':['小学','初中','高中'],'name':'Tom','age':20},{'school':['小学','初中','高中'],'name':'Jack','age':22}]";

		System.out.println("--------------java对象---------------");
		JSONObject strObj=JSONObject.fromObject(objStr);
		People p2 = (People) JSONObject.toBean(strObj,People.class);
		System.out.println(p2.getName());
		
		System.out.println("--------------java List对象---------------");
		JSONArray listObj=JSONArray.fromObject(objList);
		People[] pList = (People[]) JSONArray.toArray(listObj,People.class);
		System.out.println(pList[1].getName());
	}
}

输出结果:

--------------json对象-----------
{"school":["小学","初中","高中"],"name":"Tom","age":20}
--------------json数组-----------
[{"school":["小学","初中","高中"],"name":"Tom","age":20},{"school":["小学","初中","高中"],"name":"Jack","age":22}]
--------------java对象---------------
Tom
--------------java List对象---------------
Jack

【注】:jar包导入Json数据对象——java操作_第1张图片

在api开发中,会将对象封装到一个BaseJSON:

// BaseJSON.java
package com.cc.yonyou.funTwo; 

public class BaseJSON {
	protected int code = 0;
	protected Object result;
	
	public BaseJSON(){
	}

	
	public int getCode() {
		return code;
	}


	public void setCode(int code) {
		this.code = code;
	}


	public Object getResult() {
		return result;
	}

	public void setResult(Object result) {
		this.result = result;
	}


	@Override
	public String toString() {
		return "BaseJSON [code=" + code + ", result=" + result + "]";
	}


}
//BootMain.java

package com.cc.yonyou.funTwo;

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

import com.cc.yonyou.People;


public class BootMain {
	public static void main(String[] args){

		String[] school = {"小学","初中","高中"};
		People p = new People();
		p.setAge(20);
		p.setSchool(school);
		People p1 = new People();
		p1.setName("Jack");
		p1.setAge(22);
		p1.setSchool(school);
		List list = new ArrayList();
		list.add(p);
		list.add(p1);
		
		BaseJSON json = new BaseJSON();
		json.setResult(list);
		System.out.println(json.toString());
	}
	
}

输出:

BaseJSON [code=0, result=[com.cc.yonyou.People@15db9742, com.cc.yonyou.People@6d06d69c]]

在JavaWeb中,传递json,使用gson,下载gson.jar

//BootMain.java

package com.cc.yonyou.funThree;

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

import com.cc.yonyou.People;
import com.google.gson.Gson;

public class BootMain {
	public static void main(String[] args){
		String[] school = {"小学","初中","高中"};
		People p = new People();
		p.setAge(20);
		p.setSchool(school);
		People p1 = new People();
		p1.setName("Jack");
		p1.setAge(22);
		p1.setSchool(school);
		List list = new ArrayList();
		list.add(p);
		list.add(p1);
		
		Gson gson = new Gson();
		String json = gson.toJson(list);
		System.out.println(json);
	}
}

输出:

[{"age":20,"school":["小学","初中","高中"]},{"name":"Jack","age":22,"school":["小学","初中","高中"]}]

 

你可能感兴趣的:(json)