官方生成方法:
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();
}
}
}