package com.phone.week6.day5.json;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Test {
//规律,遇到{}就用JSONObject,遇到[]就用JSONArray
public static void main(String[] args) throws JSONException {
//http://www.weather.com.cn/data/cityinfo/101280601.html
}
//把JAVABEAN转成JSON格式的对象
private static void test6() {
Person p = new Person("张三", 20, "湖南");
Person p2 = new Person("李四", 22, "湖南");
Person p3 = new Person("王五", 23, "湖南");
List<Person> list = new ArrayList<>();
list.add(p);
list.add(p2);
list.add(p3);
Map<String, Person> map = new HashMap<String, Person>();
map.put(p.getName(), p);
map.put(p2.getName(), p2);
map.put(p3.getName(), p3);
//JSONObject json = new JSONObject(list);
JSONArray array = new JSONArray(list);
JSONObject j = new JSONObject(map);
System.out.println(array);
System.out.println(j);
}
//带对象名的JSON数组
private static void test5() throws JSONException {
String str="{persons:[{name:张三,age:20,address:深圳},{name:李四,age:21,address:上海},{name:王五,age:22,address:北京}]}";
//先把字符串转成JSON对象
JSONObject json = new JSONObject(str);
JSONArray array = json.getJSONArray("persons");
List<Person> list = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
JSONObject object=array.getJSONObject(i);
Person p= new Person(object.getString("name"), object.getInt("age"), object.getString("address"));
list.add(p);
}
for (Person person : list) {
System.out.println(person);
}
}
//解析JSON数组,不带对象名的
private static void test4() throws JSONException {
String str="[{name:张三,age:20,address:深圳},{name:李四,age:21,address:上海},{name:王五,age:22,address:北京}]";
JSONArray array = new JSONArray(str);
List<Person> list = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
JSONObject object=(JSONObject) array.get(i);
Person p= new Person(object.getString("name"), object.getInt("age"), object.getString("address"));
list.add(p);
}
for (Person person : list) {
System.out.println(person);
}
}
//对象套对象的JSON对象
private static void test3() throws JSONException {
String str = "{person1:{name:张三,age:20,address:深圳},person2:{name:李四,age:21,address:上海},person3:{name:王五,age:22,address:北京}}";
JSONObject js1 = new JSONObject(str);
JSONObject js2 = js1.getJSONObject("person1");
Person p = new Person(js2.getString("name"), js2.getInt("age"),
js2.getString("address"));
JSONObject js3 = js1.getJSONObject("person2");
Person p2 = new Person(js3.getString("name"), js3.getInt("age"),
js3.getString("address"));
JSONObject js4 = js1.getJSONObject("person3");
Person p3 = new Person(js4.getString("name"), js4.getInt("age"),
js4.getString("address"));
List<Person> list = new ArrayList<>();
list.add(p);
list.add(p2);
list.add(p3);
for (Person person : list) {
System.out.println(person);
}
}
// 带对象名的JSON对象解析,意思就是键所对应的值又是一个JSON对象
private static void test2() throws JSONException {
String str = "{person:{name:张三,age:20,address:深圳}}";
JSONObject json = new JSONObject(str); // 先把整个JSON字符串转成JSON对象
JSONObject j2 = json.getJSONObject("person"); // 通过JSON对象.getJSONObject("键名")来获取另外一个JSON对象
Person p = new Person();
p.setName(j2.getString("name"));
p.setAge(j2.getInt("age"));
p.setAddress(j2.getString("address"));
System.out.println(p);
}
// 不带对象名的JSON字符串,普通键值对的JSON对象的解析
private static void test1() throws JSONException {
String str = "{name:张三,age:20,address:深圳}";
JSONObject json = new JSONObject(str);
Person p = new Person();
p.setName(json.getString("name"));
p.setAge(json.getInt("age"));
p.setAddress(json.getString("address"));
System.out.println(p);
}
}
package com.phone.week6.day5.json;
public class Person {
private String name;
private int age;
private String address;
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Person(String name, int age, String address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
public Person() {
super();
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", address=" + address
+ "]";
}
}