json数据格式化工具:http://www.ofmonkey.com/tools/compress
- public class JSONDEMO {
- public static void main(String[] args) throws JSONException {
- String str = ”{person:[{name:’zhangsan’,age:20},{name:’lisi’,age:2},{name:’wangwu’,age:3}]}”;
- List<Person> l = new ArrayList<>();
- JSONObject ob = new JSONObject(str);
- JSONArray array = ob.getJSONArray(”person”);
- for (int i = 0; i < array.length(); i++) {
- JSONObject o = array.getJSONObject(i);
- String name = o.getString(”name”);
- int age = o.getInt(“age”);
- Person p = new Person(name, age);
- l.add(p);
- }
- for (Person person : l) {
- System.out.println(person);
- }
- }
- }
public class JSONDEMO {
public static void main(String[] args) throws JSONException {
String str = "{person:[{name:'zhangsan',age:20},{name:'lisi',age:2},{name:'wangwu',age:3}]}";
List<Person> l = new ArrayList<>();
JSONObject ob = new JSONObject(str);
JSONArray array = ob.getJSONArray("person");
for (int i = 0; i < array.length(); i++) {
JSONObject o = array.getJSONObject(i);
String name = o.getString("name");
int age = o.getInt("age");
Person p = new Person(name, age);
l.add(p);
}
for (Person person : l) {
System.out.println(person);
}
}
}
- public class Person {
- private String name;
- private int age;
- public Person(String name, int age) {
- super();
- this.name = name;
- this.age = age;
- }
- 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;
- }
- @Override
- public String toString() {
- return “Person [name=” + name + “, age=” + age + “]”;
- }
- }
public class Person {
private String name;
private int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
实例二:
- public class DemoJson {
- /**
- * {name:’zhangsan’,age:20,dept:{no:1,name:’android’}}
- *
- * 两个JSONObject
- *
- * @param args
- * @throws JSONException
- */
- public static void main(String[] args) throws JSONException {
- String str = ”{name:’zhangsan’,age:20,dept:{no:1,name:’android’}}”;
- JSONObject jsonObject = new JSONObject(str);
- //解析姓名
- String name = jsonObject.getString(”name”);
- //解析年龄
- int age = jsonObject.getInt(“age”);
- //解析部门 dept
- JSONObject jsonObject2 = jsonObject.getJSONObject(”dept”);
- int no = jsonObject2.getInt(“no”);
- String deptName = jsonObject2.getString(”name”);
- Dept dept = new Dept(no, deptName);
- Person person = new Person(name, age, dept);
- System.out.println(person);
- }
- }
public class DemoJson {
/**
* {name:'zhangsan',age:20,dept:{no:1,name:'android'}}
*
* 两个JSONObject
*
* @param args
* @throws JSONException
*/
public static void main(String[] args) throws JSONException {
String str = "{name:'zhangsan',age:20,dept:{no:1,name:'android'}}";
JSONObject jsonObject = new JSONObject(str);
//解析姓名
String name = jsonObject.getString("name");
//解析年龄
int age = jsonObject.getInt("age");
//解析部门 dept
JSONObject jsonObject2 = jsonObject.getJSONObject("dept");
int no = jsonObject2.getInt("no");
String deptName = jsonObject2.getString("name");
Dept dept = new Dept(no, deptName);
Person person = new Person(name, age, dept);
System.out.println(person);
}
}
- public class Person {
- private String name;
- private int age;
- //部门
- private Dept dept;
- public Person(String name, int age, Dept dept) {
- super();
- this.name = name;
- this.age = age;
- this.dept = dept;
- }
- 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 Dept getDept() {
- return dept;
- }
- public void setDept(Dept dept) {
- this.dept = dept;
- }
- @Override
- public String toString() {
- return “Person [name=” + name + “, age=” + age + “, dept=” + dept + “]”;
- }
- }
public class Person {
private String name;
private int age;
//部门
private Dept dept;
public Person(String name, int age, Dept dept) {
super();
this.name = name;
this.age = age;
this.dept = dept;
}
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 Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", dept=" + dept + "]";
}
}
- public class Dept {
- private int no;
- private String name;
- public int getNo() {
- return no;
- }
- public void setNo(int no) {
- this.no = no;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return “Dept [no=” + no + “, name=” + name + “]”;
- }
- public Dept(int no, String name) {
- super();
- this.no = no;
- this.name = name;
- }
- }
public class Dept {
private int no;
private String name;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Dept [no=" + no + ", name=" + name + "]";
}
public Dept(int no, String name) {
super();
this.no = no;
this.name = name;
}
}