JAVAでのJSONの使い方まとめ

4496 ワード

2つのクラス、Studentクラス、Gradeクラスを別々に作成
package test;

public class Student {
    private int id;
    private String name;
    private int age;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Student(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public Student() {
        super();
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
}
package test;

import java.util.ArrayList;

public class Grade {
    private int id;
    private String name;
    private ArrayList stus;
    public Grade(int id, String name, ArrayList stus) {
        super();
        this.id = id;
        this.name = name;
        this.stus = stus;
    }
    public Grade() {
        super();
    }
    @Override
    public String toString() {
        return "Grade [id=" + id + ", name=" + name + ", stus=" + stus + "]";
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public ArrayList getStus() {
        return stus;
    }
    public void setStus(ArrayList stus) {
        this.stus = stus;
    }
}

次はテスト方法です
package test;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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

public class jsonTest {
	public static void main(String[] args) {
		//          
        String json1="{'id':1,'name':'JAVA','stus':[{'id':101,'name':'  ','age':16}]}";
        //  
        String json2="['  ','  ','  ']";
        //    
        String json3="{'id':101,'name':'  ','age':16}";
        String json4="[{'id':101,'name':'  ','age':16,'address':'china'},{'id':102,'name':'  ','age':17},{'id':103,'name':'  ','age':16}]";

        //1
        //     ---  
        JSONObject jObject1=JSONObject.fromObject(json1);
        Grade grade=new Grade();
        grade.setId(jObject1.getInt("id"));
        grade.setName(jObject1.getString("name"));
        ArrayList stus=new ArrayList<>();
        grade.setStus(stus);
        //     ----  
        JSONArray jsonArray2=jObject1.getJSONArray("stus");
        //        ----  
        for(int i=0;i iterator=jObject1.keys();
		while (iterator.hasNext()) {
		    String key = iterator.next();
		    System.out.println("  :"+key);
		}

		Class clazz=grade.getClass();
		Field[] fields = clazz.getDeclaredFields();
		for(Field f : fields) {
			System.out.println(f.getName());//         
			System.out.println(f.getType().getName());//           
		}
		
        //2
		//      
        JSONArray jArray=JSONArray.fromObject(json2);
        ArrayList list=new ArrayList<>();
        //      
        for(int i=0;i listStu=JSONArray.toList(jaStu,Student.class);//  jaStu    Student    ,     
        System.out.println(listStu);
        
	}

}