JSON解析和GSON解析
来源:互联网 发布:java redis isinmulti 编辑:程序博客网 时间:2024/06/08 10:13
一个json对象
{“name”:”sam”,”age”:18,”weight”:60}
/**JSON/JSONObject jsonObj = new JSONObject(json1);String name = jsonObj.optString("name");int age = jsonObj.optInt("age");int weight = jsonObj.optInt("weight");/**GSON/首先我们需要一个实体类public class People{ public String name; public int mAge; public int weight;}然后就可以解析了Gson gson = new Gson();Poeple people = gson.fromJson(json1, People.class);
一个数字数组
[12,13,15]
/**JSON/ JSONArray jsonArray = new JSONArray(json2); for(int= 0; i < jsonArray.length();i++) { int age = jsonArray.optInt(i); } /**GSON/ //解析成int数组 Gson gson = new Gson(); int[] ages = gson.fromJson(json2, int[].class); //解析成Integer的List。 Gson gson = newGson(); List<Integer> ages = gson.fromJson(json2, new TypeToken<List<Integer>>(){}.getType);
json array中有object
[{“name”:”sam”,”age”:18},{“name”:”leo”,”age”:19},{“name”:”sky”, “age”:20}]
/**JSON/ JSONArray jsonArray = new JSONArray(json3); for(int= 0; i < jsonArray.length();i++) { JSONObject jsonObject = sonArray.optJSONObject(i); String name = jsonObject.optString("name"); int age = jsonObject.optInt("age"); } /**GSON/ //解析成List。 Gson gson = new Gson(); List<People> peoples = gson.fromJson(json3, new TypeToke<List<People>>(){}.getType);
0 0
- Gson 和Json解析
- Gson解析和json解析
- JSON解析和GSON解析
- json概述和gson解析
- JSON解析 GSON解析
- json解析:gson解析json
- GSON方式生成和解析json数据
- GSON方式生成和解析json数据
- google-GSON解析和生成JSON数据
- Gson解析和封装json数据
- GSON方式生成和解析json数据
- json解析库gson和fastjson使用
- GSON方式生成和解析json数据
- GSON方式生成和解析json数据
- GSON方式生成和解析json数据
- 使用Gson生成和解析json数据
- xutils和Gson解析Json数据
- 运用框架的Json和Gson解析
- 运行Maven程序报错-Dmaven.multiModuleProjectDirectory system propery is not set.
- 对称矩阵的判定 (sdut oj)
- hihocoder 1372 平方求和
- #“Machine Learning”(Andrew Ng)#Week 3_2:Logistic Regression Model
- CSS3实现动态多级菜单效果
- JSON解析和GSON解析
- JAVA中数组的内存(栈和堆)
- codevs 天梯 单词接龙 DFS
- Bmob查询获取内部类的数据
- C语言实验——求一个3*3矩阵对角线元素之和 (sdut oj)
- 【b503】篝火晚会
- codevs天梯四色问题
- 线性表之顺序表基本操作
- 树莓派上搭建语音识别服务出现error code:10407