通過gson解析Json數組(快餐)

今天剛好遇到json數組在服務器和客戶端之間的傳遞,所以正好學會json數組的解析,分享給大家

json數組代碼

[{"music_score":"3452","comment":"It is beatiful"},{"music_score":"3452","comment":"It is beatiful"},{"music_score":"3452","comment":"It is beatiful"},{"music_score":"456321","comment":" I like it"},{"music_score":"456321","comment":"dasd"}]

在客戶端通過gson解析

這裏主要是通過okhttp3接收數據,我就值粘貼解析這部分的代碼

public void onResponse(Call call, Response response) throws IOException {
                final  String res = response.body().string();
                ArrayList<Music> list = new ArrayList<>();
                Gson gson = new Gson();
                list = gson.fromJson(res, new TypeToken<List<Music>>() {
                }.getType());

                Log.v("是不是json字符串:",res);
                Log.v("kankanList",list.get(0).getComment());

            }

看一下運行結果

V/是不是json字符串:: [{"music_score":"3452","comment":"It is beatiful"},{"music_score":"3452","comment":"It is beatiful"},{"music_score":"3452","comment":"It is beatiful"},{"music_score":"456321","comment":" I like it"},{"music_score":"456321","comment":"dasd"}]
V/kankanList: It is beatiful

正確無誤,感覺通過json傳遞數據還是很方便,主要還是gson提供了方法可以編譯和解析json數據。

下一篇,想講解一下怎麼將okhttp返回的值,返回到可用的界面。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章