Android 解析Json 和Json数组 以及Gson的简单应用

释放双眼,带上耳机,听听看~!

Gson jar 下载地址 :http://download.csdn.net/download/u010637692/8348917

下载后 build一下就行了

惨考 http://www.open-open.com/solution/view/1319448346593

JSON即JavaScript Object Natation, 它是一种轻量级的数据交换格式, 与XML一样, 是广泛被采用的客户端和服务端交互的解决方案.

JSON对象: JSON中对象(Object)以”{“开始, 以”}”结束. 对象中的每一个item都是一个key-value对, 表现为”key:value”的形式, key-value对之间使用逗号分隔. 如:{“name”:”coolxing”, “age”=24, “male”:true, “address”:{“street”:”huiLongGuan”, “city”:”beijing”, “country”:”china”}}. JSON对象的key只能是string类型的, 而value可以是string,
number, false, true, null, Object对象甚至是array数组, 也就是说可以存在嵌套的情况.

JSON数组: JSON数组(array)以”[“开始, 以”]”结束, 数组中的每一个元素可以是string, number, false, true, null, Object对象甚至是array数组, 数组间的元素使用逗号分隔. 如[“coolxing”, 24, {“street”:”huiLongGuan”, “city”:”beijing”, “country”:”china”}].

JSON数据的解析:解析JSON数据, 首先需要明确待解析的是JSON Object还是JSON array, 然后需要确定采用哪种解析技术. android平台上一般有2种解析技术可供选择: android内置的org.json包和google的开源gson库. 以下将分别采用这两种技术解析JSON对象和JSON数组.

1.  采用android内置的org.json包解析JSON对象. 假设待解析的JSON数据为json = “{“name”:”coolxing”, “age”=24, “male”:true, “address”:{“street”:”huiLongGuan”, “city”:”beijing”, “country”:”china”}}”, 其中用于转义表达式中的双引号. 首先定义2个JavaBean:

先声明两个javabean

package com.yinggujing;
/**
* Created by ChenHe on 2016/6/2.
*/
public class Address {
private String street;
private String city;
private String country;
public Address() {
super();
}
public Address(String street, String city, String country) {
super();
this.street = street;
this.city = city;
this.country = country;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "Address [street=" + street + ", city=" + city + ", country="
+ country + "]";
}
}

 

package com.yinggujing;
/**
* Created by ChenHe on 2016/6/2.
*/
public class Person {
private String name;
private int age;
private boolean male;
private Address address;
public Person() {
super();
}
public Person(String name, int age, boolean male, Address address) {
super();
this.name = name;
this.age = age;
this.male = male;
this.address = 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 boolean isMale() {
return male;
}
public void setMale(boolean male) {
this.male = male;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", male=" + male
+ ", address=" + address + "]";
}
}

java代码

package com.yinggujing;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yinggujing.FragListBean.ChildWarningArcherListBean;
import java.lang.reflect.Type;
import java.util.List;
/**
* Created by ChenHe on 2016/6/2.
*/
public class GsonText extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//数据源 一一对应的 分别对应两种方式 json和Gson解析
String json = "[{"name":"coolxing", "age"=24, "male":true, "address":{"street":"huiLongGuan", "city":"beijing", "country":"china"}}, {"name":"min", "age"=20, "male":false, "address":{"street":"heiShiJiao", "city":"daLian", "country":"china"}}]";
String json1 = "{"name":"coolxing", "age"=24, "male":true, "address":{"street":"huiLongGuan", "city":"beijing", "country":"china"}}";
String json2 = "[{"name":"coolxing", "age"=24, "male":true, "address":{"street":"huiLongGuan", "city":"beijing", "country":"china"}}, {"name":"min", "age"=20, "male":false, "address":{"street":"heiShiJiao", "city":"daLian", "country":"china"}}]";
//        try {
//            List<Person> persons = new ArrayList<Person>();
//            JSONArray jsonArray = new JSONArray(json);
//            for(int i = 0; i < jsonArray.length(); i++)
//            {
//                JSONObject jsonObject = jsonArray.getJSONObject(i);
//                String name = jsonObject.getString("name");
//                int age = jsonObject.getInt("age");
//                boolean male = jsonObject.getBoolean("male");
//                JSONObject addressJSON = jsonObject.getJSONObject("address");
//                String street = addressJSON.getString("street");
//                String city = addressJSON.getString("city");
//                String country = addressJSON.getString("country");
//                Address address = new Address(street, city, country);
//                Person person = new Person(name, age, male, address);
//                persons.add(person);
//            }
//            System.out.println(persons);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//        Gson gson = new Gson();
//        Person person = gson.fromJson(json1, Person.class);
//        System.out.println(person);
//        try {
//            List<Person> persons = new ArrayList<Person>();
//            JSONArray jsonArray = new JSONArray(json2);
//            for(int i = 0; i < jsonArray.length(); i++) {
//                JSONObject jsonObject = jsonArray.getJSONObject(i);
//                String name = jsonObject.getString("name");
//                int age = jsonObject.getInt("age");
//                boolean male = jsonObject.getBoolean("male");
//                JSONObject addressJSON = jsonObject.getJSONObject("address");
//                String street = addressJSON.getString("street");
//                String city = addressJSON.getString("city");
//                String country = addressJSON.getString("country");
//                Address address = new Address(street, city, country);
//                Person person = new Person(name, age, male, address);
//                persons.add(person);
//            }
//            System.out.println(persons);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
Gson gson = new Gson();
Type listType = new TypeToken<List<ChildWarningArcherListBean>>(){}.getType();
List<ChildWarningArcherListBean> persons = gson.fromJson(json2, listType);
System.out.println(persons);
}
}

人已赞赏
Android文章

Android Spinner 简单应用

2020-3-27 22:03:05

Android文章

Android开发EditText 设置光标颜色 删除下划线 边框

2020-3-27 23:18:08

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索