前言:
利用原生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());