GSON解析数据(包括json解析)

GSOn作用:
把需要解析的数据(字符串)转换成对象或把对象转换为 字符串

使用JSONView工具:
把需要解析的数据放在assets目录下:

image.png

image.png

image.png

xml:
···

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.gson.MainActivity">

···
------------------------------------代码---------------
···
package com.example.wangye.zknewstest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;

public class MainActivity extends AppCompatActivity {
Button btNative, btGson;
TextView tx;
String json = "";
String content = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}

public void init(){
    try {
        InputStream is = getAssets().open("zk_new_json.txt");
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        byte buffer[] = new byte[512];
        int length = -1;
        while(   ( length = is.read(buffer)) != -1){
            bs.write(buffer,0,length);
            bs.flush();
        }
        is.close();
        bs.close();
        json = bs.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    btNative = (Button) findViewById(R.id.button);
    btNative.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            parseJsonByNative(json);
        }
    });
    btGson = (Button) findViewById(R.id.button2);
    btGson.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            parseJsonByGson(json);
        }
    });
    tx = (TextView) findViewById(R.id.textView);
}

public void parseJsonByNative(String json){
    try {
        JSONObject jsonObject = new JSONObject(json);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray course = data.getJSONArray("course");
        for (int i = 0; i < course.length(); i++) {
            JSONObject courseObject = course.getJSONObject(i);
            JSONArray courseDetail = courseObject.getJSONArray("courseDetail");

            String professional = courseDetail.getJSONObject(0).getString("professional");
            content += "专业:"+professional+"\n";
            String university = courseDetail.getJSONObject(1).getString("university");
            content += "大学:"+university+"\n";
            String brief = courseDetail.getJSONObject(2).getString("brief");
            content += "专业介绍:"+brief+"\n";
            String region = courseDetail.getJSONObject(3).getString("region");
            content += "地区:"+region+"\n";
            String require = courseDetail.getJSONObject(4).getString("require");
            content += "要求:"+require+"\n";
            String frequency = courseDetail.getJSONObject(5).getString("frequency");
            content += "考试周期:"+frequency+"\n";
            String subject = courseDetail.getJSONObject(6).getString("subject");
            content += "考试科目:"+subject+"\n";
            JSONArray exams = courseDetail.getJSONObject(7).getJSONArray("exams");
            for (int j = 0; j < exams.length(); j++) {
                JSONObject examsObject = exams.getJSONObject(j);
                JSONArray examDetail = examsObject.getJSONArray("examDetail");
                String examTimeYear = examDetail.getJSONObject(0).getString("examTimeYear");
                content += "考试年份:"+examTimeYear+"\n";
                String examTimeMonth = examDetail.getJSONObject(1).getString("examTimeMonth");
                content += "考试月份:"+examTimeMonth+"\n";
                String signUpTime = examDetail.getJSONObject(2).getString("signUpTime");
                content += "报名时间:"+signUpTime+"\n";
                String examScore = examDetail.getJSONObject(4).getString("examScore");
                if(examScore.equals("null")){
                    content += "公布成绩:"+"待定"+"\n";
                }else{
                    content += "公布成绩:"+examScore+"\n";
                }
                JSONArray examDay = examDetail.getJSONObject(3).getJSONArray("examDay");
                for (int k = 0; k < examDay.length(); k++) {
                    JSONObject examDayObject = examDay.getJSONObject(k);
                    JSONArray day = examDayObject.getJSONArray("day");
                    String timeDay = day.getJSONObject(0).getString("timeDay");
                    content += "考试日期:"+timeDay+"\n";
                    JSONArray timeUp = day.getJSONObject(1).getJSONArray("timeUp");
                    String timeUpString="";
                    if(timeUp.length() == 1){
                        timeUpString = "暂无考试安排";
                    }
                    if(timeUp.length() == 2){
                        timeUpString = "考试时间:"+timeUp.getString(0)+"\n考试科目:"+timeUp.getString(1);
                    }
                    if(timeUp.length() == 3){
                        timeUpString = "考试时间:"+timeUp.getString(0)
                                +"\n考试科目1:"+timeUp.getString(1)
                                +"\n考试科目2:"+timeUp.getString(2);
                    }
                    content += timeUpString+"\n";
                    JSONArray timeDown = day.getJSONObject(2).getJSONArray("timeDown");
                    String timeDownString="";
                    if(timeDown.length() == 1){
                        timeDownString = "暂无考试安排";
                    }
                    if(timeDown.length() == 2){
                        timeDownString = "考试时间:"+timeDown.getString(0)+"\n考试科目:"+timeDown.getString(1);
                    }
                    if(timeDown.length() == 3){
                        timeDownString = "考试时间:"+timeDown.getString(0)
                                +"\n考试科目1:"+timeDown.getString(1)
                                +"\n考试科目2:"+timeDown.getString(2);
                    }
                    content += timeDownString+"\n";
                }
            }
        }
        tx.setText("解析到的数据为:\n"+content);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void parseJsonByGson(String json){
    String data = "";
    Gson gson = new Gson();
    News news = gson.fromJson(json,News.class);
    List courseBean = news.getData().getCourse();
    for (int i = 0; i < courseBean.size(); i++) {
        List courseDetailBean =
                courseBean.get(i).getCourseDetail();
        data += "专业:"+courseDetailBean.get(0).getProfessional()+"\n";
        data += "大学:"+courseDetailBean.get(1).getUniversity()+"\n";
        data += "专业介绍:"+courseDetailBean.get(2).getBrief()+"\n";
        data += "地区:"+courseDetailBean.get(3).getRegion()+"\n";
        data += "要求:"+courseDetailBean.get(4).getRequire()+"\n";
        data += "考试周期:"+courseDetailBean.get(5).getFrequency()+"\n";
        data += "考试科目:"+courseDetailBean.get(6).getSubject()+"\n";
        List examsBean =
                courseDetailBean.get(7).getExams();
        for (int j = 0; j < examsBean.size(); j++) {
             List examDetailBean =
                     examsBean.get(j).getExamDetail();
            data += "考试年份:"+examDetailBean.get(0).getExamTimeYear()+"\n";
            data += "考试月份:"+examDetailBean.get(1).getExamTimeMonth()+"\n";
            data += "登记时间:"+examDetailBean.get(2).getSignUpTime()+"\n";
            data += "公布成绩:"+examDetailBean.get(4).getExamScore()+"\n";
            List examDayBean =
                    examDetailBean.get(3).getExamDay();
            for (int k = 0; k < examDayBean.size(); k++) {
                List dayBean =
                        examDayBean.get(k).getDay();
                data += "考试日期:"+dayBean.get(0).getTimeDay()+"\n";
                List timeUp = dayBean.get(1).getTimeUp();
                if(timeUp.size() == 1){
                    data += "上午暂无考试安排";
                }
                if(timeUp.size() == 2){
                    data += "考试时间:"+timeUp.get(0)+"\n考试科目:"+timeUp.get(1);
                }
                if(timeUp.size() == 3){
                    data += "考试时间:"+timeUp.get(0)+"\n考试科目1:"+timeUp.get(1)+"\n考试科目2:"+timeUp.get(2);
                }
                List timeDown = dayBean.get(2).getTimeDown();
                if(timeDown.size() == 1){
                    data += "上午暂无考试安排";
                }
                if(timeDown.size() == 2){
                    data += "考试时间:"+timeDown.get(0)+"\n考试科目:"+timeDown.get(1);
                }
                if(timeDown.size() == 3){
                    data += "考试时间:"+timeDown.get(0)+
                            "\n考试科目1:"+timeDown.get(1)+
                            "\n考试科目2:"+timeDown.get(2);
                }
            }
        }
    }

    tx.setText("Gson解析:\n"+data);

}

}

···

你可能感兴趣的:(GSON解析数据(包括json解析))