json数据示例
[ { "id": 1, "imagePath": "http://192.168.10.165:8080/f1.jpg", "name": "大虾1", "price": 12.3 }, { "id": 2, "imagePath": "http://192.168.10.165:8080/f2.jpg", "name": "大虾2", "price": 12.5 } ]实现解析
private void jsonToJavaBeanListByGson() { String json = "[\n" + " {\n" + " \"id\": 1,\n" + " \"imagePath\": \"http://192.168.10.165:8080/f1.jpg\",\n" + " \"name\": \"大虾1\",\n" + " \"price\": 12.3\n" + " },\n" + " {\n" + " \"id\": 2,\n" + " \"imagePath\": \"http://192.168.10.165:8080/f2.jpg\",\n" + " \"name\": \"大虾2\",\n" + " \"price\": 12.5\n" + " }\n" + "]"; Gson gson = new Gson(); List<ShopBean> list = gson.fromJson(json, new TypeToken<List<ShopBean>>(){}.getType()); tv1.setText(json); tv2.setText(list.toString()); }将java对象转换为json字符串
Api介绍
String toJson(Object src);实现转换
private void javaBeanToJsonByGson() { DemoBean demoBean = new DemoBean("0", "haha", "是你答复我还是额咯结尾"); Gson gson = new Gson(); String json = gson.toJson(demoBean); tv1.setText(demoBean.toString()); tv2.setText(json); }将javaBean的集合对象转换成json数组
Api介绍
String toJson(Object src);实现转换
private void javaBeanListToJsonByGson() { List<ShopBean> list = new ArrayList<>(); ShopBean shopBean1 = new ShopBean("http://www.baidu.com", 12.3, "鱼肉", 1); ShopBean shopBean2 = new ShopBean("http://www.baidu.com", 15.7, "鸡腿", 2); list.add(shopBean1); list.add(shopBean2); Gson gson = new Gson(); String json = gson.toJson(list); tv1.setText(list.toString()); tv2.setText(json); }FastJson框架解析
将json对象字符串转换为javaBean
Api介绍(compile ‘com.alibaba:fastjson:1.2.31’)
Json.parseObject(String json, Class<T> classOfT);实现解析
private void javaBeanListToJsonByFastJson() { 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" + "}\n"; ShopBean shopBean = JSON.parseObject(json, ShopBean.class); tv1.setText(json); tv2.setText(shopBean.toString()); }将json数组字符串转换成javaBean的List集合
Api介绍
Json.List<T> parseArray(String json,Class<T> classOfT);实现解析
private void javaBeanListToJsonByFastJson() { 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" + "}\n"; ShopBean shopBean = JSON.parseObject(json, ShopBean.class); tv1.setText(json); tv2.setText(shopBean.toString()); }将java对象转换为json字符串
Api介绍
String Json.toJSONString(Object object);实现解析
private void javaBeanToJsonByFastJson() { DemoBean demoBean = new DemoBean("0", "haha", "是你答复我还是额咯结尾"); String jsonString = JSON.toJSONString(demoBean); tv1.setText(demoBean.toString()); tv2.setText(jsonString); }将javaBean的集合对象转换成json数组
Api介绍
String Json.toJSONString(Object object);实现解析
private void javaBeanListToJsonByFastJson() { List<ShopBean> list = new ArrayList<>(); ShopBean shopBean1 = new ShopBean("http://www.baidu.com", 12.3, "鱼肉", 1); ShopBean shopBean2 = new ShopBean("http://www.baidu.com", 15.7, "鸡腿", 2); list.add(shopBean1); list.add(shopBean2); String jsonString = JSON.toJSONString(list); tv1.setText(list.toString()); tv2.setText(jsonString); } 五、Demo下载:源码链接
以上就是Android框架之路Json解析一篇就够(原生技术+Gson+FastJson)的全文介绍,希望对您学习Android应用开发有所帮助. 这些内容可能对你也有帮助更多Android开发可查看Android开发列表页。