生成JSON資料--官方方法

Java仗劍走天涯發表於2017-05-05

官方生成方法:

1)需要什麼就給什麼,要屬性就給屬性,要物件就給物件,要集合就給集合

2)新增都是使用put()方法

要求:

1.生成如下JSON資料:
{“age”:4,”name”:”zhagnsan”}

2.生成如下JSON資料:
[{“age”:4,”name”:”zhangsan”},{“age”:5,”name”:”lis”},{“age”:6,”name”:”wangw”}]

3.生成如下JSON資料:
{“no”:1,”name”:”android”,”employees”:[{“age”:3,”name”:”zhangsan”},{“age”:5,”name”:”lisi”},{“age”:2,”name”:”wangw”}]}


第一題程式碼:

package com.qf.demo5;

import org.json.JSONException;
import org.json.JSONObject;

public class Test {

    public static void main(String[] args) {
        //生成如下JSON資料:{"age":4,"name":"zhagnsan"}
        JSONObject object = new JSONObject();// 產生json資料   不需要引數
        try {
            object.put("name", "zhagnsan");
            object.put("age", 4);
            System.out.println(object);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

第二題程式碼:

Person類

package com.qf.demo2;

public class Person {

    private int age;
    private String name;
    public Person(int age, String name) {
        super();
        this.age = age;
        this.name = name;
    }
    public Person() {
        super();
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Person [age=" + age + ", name=" + name + "]";
    }


}
package com.qf.demo5;

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

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.qf.demo2.Person;

public class Test2 {
    public static void main(String[] args) {
        //生成如下JSON資料:[{"age":4,"name":"zhangsan"},{"age":5,"name":"lis"},{"age":6,"name":"wangw"}]
        List<Person> list = new ArrayList<>();
        list.add(new Person(4, "zhangsan"));
        list.add(new Person(5, "lis"));
        list.add(new Person(6, "wangw"));

        JSONArray array = new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            JSONObject object = new JSONObject();
            try {
                object.put("name", list.get(i).getName());
                object.put("age", list.get(i).getAge());
                array.put(object);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println(array);
    }
}

第三題程式碼:

Person類:

package com.qf.demo2;

public class Person {

    private int age;
    private String name;
    public Person(int age, String name) {
        super();
        this.age = age;
        this.name = name;
    }
    public Person() {
        super();
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Person [age=" + age + ", name=" + name + "]";
    }


}
package com.qf.demo5;

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

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.qf.demo2.Person;

public class Test3 {

    public static void main(String[] args) {
        //生成如下JSON資料:{"no":1,"name":"android","employees":[{"age":3,"name":"zhangsan"},{"age":5,"name":"lisi"},{"age":2,"name":"wangw"}]}
        JSONObject object = new JSONObject();
        try {
            object.put("no", 1);
            object.put("name", "android");

            List<Person> persons = new ArrayList<>();
            persons.add(new Person(3, "zhangsan"));
            persons.add(new Person(5, "lisi"));
            persons.add(new Person(2, "wangw"));

            object.put("employees", persons);
            System.out.println(object);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

相關文章