json的解析方式

1:用系统自带的JSONArray和JSONObject进行解析

public class MainActivity extends AppCompatActivity {
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        OkHttpClient okHttpClient = new OkHttpClient();
                        Request request = new Request.Builder()
                                .url("http://192.168.43.24:11403/sss/he.json")
                                .build();
                        try {
                            Response response = okHttpClient.newCall(request).execute();
                            String responsedata = response.body().string();
                            ParseJson(responsedata);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                ).start();
            }
        });
    }

    private void ParseJson(String responsedata) throws Exception {
        JSONArray jsonArray=new JSONArray(responsedata);
        for(int i=0;i

2:用gson进行解析(gson的具体使用方法)

public class MainActivity extends AppCompatActivity {
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        OkHttpClient okHttpClient = new OkHttpClient();
                        Request request = new Request.Builder()
                                .url("http://192.168.43.24:11403/sss/he.json")
                                .build();
                        try {
                            Response response = okHttpClient.newCall(request).execute();
                            String responsedata = response.body().string();
                            ParseJson(responsedata);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                ).start();
            }
        });
    }

    private void ParseJson(String responsedata) throws Exception {
        Gson gson=new Gson();
        //gson.fromJson(responsedata,shuju.class);//这条代码只能解析一个shuju对象
        List list=gson.fromJson(responsedata,new TypeToken>(){}.getType());//这条代码可以解析json数组,生成多个shuju对象
        for(shuju shuju:list){
            Log.i("xinxi",shuju.toString());
        }
    }
}
/*tomcat上的数据为:
[{"id":"5","version":"5.0","name":"qq"},{"id":"6","version":"6.0","name":"yy"},{"id":"7","version":"7.0","name":"mm"}]
经过解析,输出结果为:
11-16 17:41:30.006 23465-23576/com.example.liang.myapplication I/xinxi: shuju{id='5', name='qq', version='5.0'}
11-16 17:41:30.006 23465-23576/com.example.liang.myapplication I/xinxi: shuju{id='6', name='yy', version='6.0'}
11-16 17:41:30.006 23465-23576/com.example.liang.myapplication I/xinxi: shuju{id='7', name='mm', version='7.0'}*/

在shuju.java文件中

public class shuju {
    private String id;
    private String name;
    private String version;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    @Override
    public String toString() {
        return "shuju{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", version='" + version + '\'' +
                '}';
    }
}

你可能感兴趣的:(json的解析方式)