Android-解析Json(Gson-FastJson)

Gson解析

//1.导包
com.google.code.gson:gson:2.8.0
//2.建两个实体类
BigPerson 
public class BigPerson {
    private int list;
    private List persons;

    public BigPerson() {
    }

    public BigPerson(int list, List persons) {
        this.list = list;
        this.persons = persons;
    }

    public int getList() {
        return list;
    }

    public void setList(int list) {
        this.list = list;
    }

    public List getPersons() {
        return persons;
    }

    public void setPersons(List persons) {
        this.persons = persons;
    }
}


SmallPerson:

public class SmallPerson implements Parcelable{
    private int pid;
    private String pname;
    private int page;

    public SmallPerson() {
    }

    public SmallPerson(int pid, String pname, int page) {
        this.pid = pid;
        this.pname = pname;
        this.page = page;
    }

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(pid);
        parcel.writeString(pname);
        parcel.writeInt(page);
    }


    public static final Parcelable.Creator CREATOR=new Parcelable.Creator(){

        @Override
        public SmallPerson createFromParcel(Parcel parcel) {
            SmallPerson smallPerson=new SmallPerson();
            smallPerson.setPid(parcel.readInt());
            smallPerson.setPname(parcel.readString());
            smallPerson.setPage(parcel.readInt());
            return smallPerson;
        }

        @Override
        public SmallPerson[] newArray(int i) {
            return new SmallPerson[i];
        }
    };

    @Override
    public String toString() {
        return "SmallPerson{" +
                "pid=" + pid +
                ", pname='" + pname + '\'' +
                ", page=" + page +
                '}';
    }
}
 
  Activity:     
public void parsejson(View view) {
        new MyTask().execute();

    }
         
 class MyTask extends AsyncTask {
        @Override
        protected Object doInBackground(Object[] params) {
            try {
                URL url = new URL("http://192.168.42.58:8080/examples/person.json");
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.setConnectTimeout(5000);
                //获取结果码
                int code = httpURLConnection.getResponseCode();
                if (code == 200) {
                    InputStream is = httpURLConnection.getInputStream();
                    //测试
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
                    String str = null;
                    StringBuffer stringBuffer = new StringBuffer();
                    while ((str = bufferedReader.readLine()) != null) {
                        stringBuffer.append(str);}
                    Log.i("test", stringBuffer.toString());
                    //解析Json,
                    //使用Gson解析json
                   Gson gson=new Gson();
                   BigPerson bigPerson=gson.fromJson(stringBuffer.toString(),BigPerson.class);
                    Log.i("test",bigPerson.getList()+"");
                    List persons=bigPerson.getPersons();
                    for (SmallPerson person : persons) {
                       Log.i("test",person.toString());
                   }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();}
            return null;
           }
        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
        }}


FastJson解析

加Jar包:fastjson-1.2.3.jar

在Gson下改代码,将Gson解析改为fastjson

                  //FastJson
                    BigPerson bigPerson=JSON.parseObject(stringBuffer.toString(),BigPerson.class);
                    Log.i("test",bigPerson.getList()+"");
                    List persons=bigPerson.getPersons();
                    for (SmallPerson person : persons) {
                        Log.i("test",person.toString());
                    }

你可能感兴趣的:(json,gson,android)