Android原生技术解析JSON学习记录

前言:

利用原生Android技术解析四种JSON,将JSON转化为JAVA,主要分为四种:将JSON对象转化为JAVA对象、将JSON数组转化为JAVA数组、复杂JSON数据解析、特殊JSON数据解析。

一:将JSON对象转化为JAVA对象

用API:

JSONObject(String json) : 将json字符串解析为json对象

格式:

Xxx.getXxx(String name):根据name,在json对象中得到对应的Value
Xxx.optXxx(String name):根据name,在json对象中得到对应的Value

注意:

Xxx.optXxx(String name)方法会得到在对应的key中的值不存在的时候返回一个空字符串或者返回你指定的默认值,但是Xxx.getXxx(String name)方法会出现空指针异常的错误。

(1)获取json数据

String json = "{\n" +
                    "\t\"id\":2,\"name\":\"大虾\",\n" +
                    "\t\"price\":12.3,\n" +
                    "\t\"imagePath\":\"http://192.168.10.165:8080/L05 Server/images/f1.jpg\"\n" +
                    "}";

(2)解析json数据

ShopInfo shopInfo = null;
try {
                jsonObject = new JSONObject(json);
                int id = jsonObject.optInt("id");
                String name = jsonObject.optString("name");
                double price = jsonObject.optDouble("price");
                String imagePath = jsonObject.optString("imagePath");
                shopInfo = new ShopInfo(id,name,price,imagePath);
            } catch (JSONException e) {
                e.printStackTrace();
            }

(3)显示json数据

tv_native_orignal.setText(json);
tv_native_last.setText(shopInfo.toString());

二:将JSON数组转化为JAVA数组

用API:

JSONArray(String json) : 将json字符串解析为jsons数组

格式:

Xxx.getXxx(int index):根据下标得到json数组中对应的Value
Xxx.optXxx(int index):根据下标得到json数组中对应的Value

注意:

Xxx.optXxx(String name)方法会得到在对应的key中的值不存在的时候返回一个空字符串或者返回你指定的默认值,但是Xxx.getXxx(String name)方法会出现空指针异常的错误。

(1)获取json数据

String json ="[\n" +
                    "   {\n" +
                    "\t\"id\":1,\n" +
                    "\t\"imagePath\":\"http://192.168.10.165:8080/f1.jpg\",\n" +
                    "\t\"name\":\"大虾1\",\n" +
                    "\t\"price\":12.3\n" +
                    "   }\n" +
                    "   {\n" +
                    "\t\"id\":2,\n" +
                    "\t\"imagePath\":\"http://192.168.10.165:8080/f2.jpg\",\n" +
                    "\t\"name\":\"大虾2\",\n" +
                    "\t\"price\":12.5\n" +
                    "   }\n" +
                    "]";

(2)解析json数据

JSONArray jsonArray = null;
ShopInfo shopInfo = null;
List<ShopInfo> shopInfoList = new ArrayList<>();
            try {
                jsonArray = new JSONArray(json);
                for (int i = 0; i < jsonArray.length();i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    if (jsonObject != null){
                        int id = jsonObject.optInt("id");
                        String name = jsonObject.optString("name");
                        double price = jsonObject.optDouble("price");
                        String imagePath = jsonObject.optString("imagePath");
                        shopInfo = new ShopInfo(id,name,price,imagePath);
                        shopInfoList.add(shopInfo);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

(3)显示json数据

tv_native_orignal.setText(json);
 tv_native_last.setText(shopInfoList.toString());

三:复杂json数据解析

利用GsonFormat工具生成对象类

如下图所示,先下载GsonFormat工具
在这里插入图片描述
(1)获取json数据

String json = "{\n" +
                    "\t\"data\":{\n" +
                    "\t\t\"count\":5,\n" +
                    "\t\t\"items\":[\n" +
                    "\t\t\t{\n" +
                    "\t\t\t\"id\":45,\n" +
                    "\t\t\t\"title\":\"坚果\"\n" +
                    "\t\t\t},\n" +
                    "\t\t\t{\n" +
                    "\t\t\t\"id\":132,\n" +
                    "\t\t\t\"title\":\"炒货\"\n" +
                    "\t\t\t},\n" +
                    "\t\t\t{\n" +
                    "\t\t\t\"id\":166,\n" +
                    "\t\t\t\"title\":\"蜜饯\"\n" +
                    "\t\t\t},\n" +
                    "\t\t\t{\n" +
                    "\t\t\t\"id\":195,\n" +
                    "\t\t\t\"title\":\"果脯\"\n" +
                    "\t\t\t},\n" +
                    "\t\t\t{\n" +
                    "\t\t\t\"id\":196,\n" +
                    "\t\t\t\"title\":\"礼盒\"\n" +
                    "\t\t\t}\n" +
                    "\t\t\t]\n" +
                    "\t\t},\n" +
                    "\t\t\"rs_code\":\"1000\",\n" +
                    "\t\t\"rs_msg\":\"success\"\n" +
                    "}";

(2)解析json数据

DataInfo dataInfo = new DataInfo();
JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(json);
                //第一层解析
                JSONObject data = jsonObject.optJSONObject("data");
                int rs_code = jsonObject.optInt("rs_code");
                String rs_msg = jsonObject.optString("success");
                //第一层封装
                DataInfo.DataBean databean = new DataInfo.DataBean();
                dataInfo.setData(databean);
                dataInfo.setRs_code(rs_code);
                dataInfo.setRs_msg(rs_msg);

                //第二层解析
                int count = data.optInt("count");
                JSONArray jsonArray = data.optJSONArray("items");
                //第二次封装
                databean.setCount(count);
                List<DataInfo.DataBean.ItemsBean> item = new ArrayList<>();
                databean.setItems(item);

                //第三层解析
                for (int i = 0; i < jsonArray.length(); i++){
                    JSONArray jsonArray1 = jsonArray.optJSONArray(i);
                    if (jsonArray1 != null){
                        int id = jsonArray1.optInt(Integer.parseInt("id"));
                        String title = jsonArray1.optString(Integer.parseInt("title"));
                        //第三层封装
                        DataInfo.DataBean.ItemsBean bean = new DataInfo.DataBean.ItemsBean();
                        bean.setId(id);
                        bean.setTitle(title);
                        item.add(bean);
                    }
                }

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

(3)解析json数据

tv_native_orignal.setText(json);
tv_native_last.setText(dataInfo.toString());

四:特殊JSON数据解析

(1)获取json数据

String json = "{\n" +
                    "\t\"code\":0,\n" +
                    "\t\"list\":{\n" +
                    "\t\t\"0\":{\n" +
                    "\t\t\t\"aid\":\"6008965\",\n" +
                    "\t\t\t\"author\":\"哔哩哔哩动画\",\n" +
                    "\t\t\t\"coins\":\"170\",\n" +
                    "\t\t\t\"copyright\":\"Copy\",\n" +
                    "\t\t\t\"create\":\"2016-08-25 21:34\"\n" +
                    "\t\t},\n" +
                    "\t\t\"1\":{\n" +
                    "\t\t\t\"aid\":\"6008938\",\n" +
                    "\t\t\t\"author\":\"哔哩哔哩动画\",\n" +
                    "\t\t\t\"coins\":\"404\",\n" +
                    "\t\t\t\"copyright\":\"Copy\",\n" +
                    "\t\t\t\"create\":\"2016-08-25 21:33\"\n" +
                    "\t\t}\n" +
                    "\t}\n" +
                    "}";

(2)解析json数据

FilmInfo filmInfo = new FilmInfo();
            try {
                JSONObject jsonObject = new JSONObject(json);
                //第一层解析
                JSONObject list = jsonObject.optJSONObject("list");
                String code = jsonObject.optString("code");
                //第一层封装
                filmInfo.setCode("code");
                List<FilmInfo.ListBean> list1 = new ArrayList<>();
                filmInfo.setList((FilmInfo.ListBean) list1);

                //第二层解析
                for (int i = 0;i < list.length();i++){
                    JSONObject jsonObject1 = list.optJSONObject(String.valueOf(i));
                    if (jsonObject1 != null){
                        String aid = jsonObject1.optString("aid");
                        String author = jsonObject1.optString("author");
                        int coins = jsonObject1.optInt("coins");
                        String copyright = jsonObject1.optString("copyright");
                        String create = jsonObject1.optString("create");
                        //第二层封装
                        FilmInfo.ListBean listbean = new FilmInfo.ListBean();
                        listbean.setAid("aid");
                        listbean.setAuthor("author");
                        listbean.setCoins("coin");
                        listbean.setCopyright("copyright");
                        listbean.setCreate("create");
                        list1.add(listbean);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

(3)显示json数据

tv_native_orignal.setText(json);
tv_native_last.setText(filmInfo.toString());
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值