使用Gson解析json格式的字符串的正确方式
来源:互联网 发布:mac如何打开msg文件 编辑:程序博客网 时间:2024/04/30 14:04
开发环境:Android Studio 开始前先添加google的依赖包,添加方法:在builder.gradle文件中添加'com.google.code.gson:gson:2.3.1'
Eclipse开发环境的需要对应的jar包,这里就不提供下载了,直接去google下载就OK。
假如有这么一个字符串:
{ "title": "get_secret_chatGroup_isSaved", "my_ip_": "112.95.153.98", "data": { "2e920892-1f38-419a-81eb-1d6adae474cd": { "member": [ 43905, 44099, 42969, 44088 ], "svr_guid": 3000001187, "name": "123" }, "c14b7992-9e70-4425-88ad-cb286ee226b6": { "member": [ 43905, 44088, 44099 ], "svr_guid": 3000001187, "name": "哈哈" } }, "retcode_": "0"}
"member": [ 43905, 44099, 42969, 44088 ], "svr_guid": 3000001187, "name": "123"这部分其实就可以直接映射成对象,我将其映射为ContactsChatroom对象
下面开始解析步骤:
1.将字符串转为JsonObject对象:JsonObject obj = new JsonParser().parse(json).getAsJsonObject();
2.需要获取某个键的值:int code = obj.get("retcode_").getAsInt();
(这里获取的就是retcode_键的值,并将其转为int类型;提供了多种方法getAsString,getAsLong等)
3.获取data键的值。由于data键的值是一个JsonObject,因此使用:JsonObject data = obj.get("data").getAsJsonObject();
4.由于data的值不是一个JsonArray,因此不能遍历;但是它由多个JsonObject 对象组成,那么可以将多个JsonObject 对象转化为集合。
正好,JsonObject提供了一个接口entrySet(),将其转为set集合
Set<Map.Entry<String, JsonElement>> entrySet = data.entrySet();
5.通过遍历集合,又根据key获取值
for (Map.Entry<String, JsonElement> entry : entrySet) {
JsonObject jObject = data.getAsJsonObject(entry.getKey());
//这个时候的jObject就是我们真正需要的数据
//然后通过new Gson().fromJson(jObject,ContactsChatroom.class)方法将jObject对象解析为ContactsChatroom对象
ContactsChatroom cc = gson.fromJson(jObject,ContactsChatroom.class);
注意:如果在javabean(ContactsChatroom)中有字段是非字符串类型,则无法解析,会给相应的字段一个默认值
这个时候需要对这个字段做特殊解析。如:ContactsChatroom的pcId字段是long类型,就需要就行如下的操作:
cc.pcId = jObject.get("svr_guid").getAsLong();
}
JsonObject jsonObject = new JsonParser().parse(mGsonString).getAsJsonObject();String title = jsonObject.get("title").getAsString();String my_ip_ = jsonObject.get("my_ip_").getAsString();int recode = jsonObject.get("retcode_").getAsInt();Log.d("mytest","title=" + title);Log.d("mytest","my_ip_=" + my_ip_);Log.d("mytest","recode=" + recode);Gson gson = new Gson();JsonObject data = jsonObject.get("data").getAsJsonObject();Set<Entry<String, JsonElement>> entrySet = data.entrySet();for(Entry<String, JsonElement> type : entrySet){ JsonObject obj = data.getAsJsonObject(type.getKey()); ContactRoom cr = gson.fromJson(obj,ContactRoom.class); Log.d("mytest","type.getKey()=" + type.getKey()); Log.d("mytest","cr=" + cr.name + ":" + cr.svr_guid + ":" + cr.member);}
又如另外一种格式的json字符串:
{ "res": "1", "title": "get_msg_setting_info", "groupList": [ { "ses_id": "F9E0147B-3444-413F-A568-C2D49464CCFB" }, { "ses_id": "2b694b46-b839-4364-905f-b352651c53a9" }, { "ses_id": "9faace20-25a8-47cb-bff3-5fb3223d7ab5" }, { "ses_id": "c14b7992-9e70-4425-88ad-cb286ee226b6" }, { "ses_id": "2e920892-1f38-419a-81eb-1d6adae474cd" } ], "my_ip_": "112.95.153.98", "retcode_": "0", "contactsList": [ { "contact": 1122 } ]}将ses_id字段的值解析出来:
JsonObject jObject = new JsonParser().parse(str).getAsJsonObject();JsonArray array = jObject.get("groupList").getAsJsonArray();for(JsonElement jsonElement : array){ JsonObject jo = jsonElement.getAsJsonObject(); String s = jo.get("ses_id").getAsString(); Log.d("mytest","mytest=" + s);}
再例如,还有下面的字符串:
{" + "\"error\": 0," + "\"status\": \"success\"," + "\"date\": \"2014-05-10\"," + "\"results\":[" + "{" + "\"currentCity\":\"南京\"," + "\"weather_data\":[" + "{" + "\"date\":\"周六(今天, 实时:19℃)\"," + "\"dayPictureUrl\": \"http://api.map.baidu.com/images/weather/day/dayu.png\"," + "\"nightPictureUrl\": \"http://api.map.baidu.com/images/weather/night/dayu.png\"," + "\"weather\": \"大雨\"," + "\"wind\": \"东南风5-6级\"," + "\"temperature\": \"18℃\"" + "}," + "{" + "\"date\": \"周日\"," + "\"dayPictureUrl\": \"http://api.map.baidu.com/images/weather/day/zhenyu.png\"," + "\"nightPictureUrl\": \"http://api.map.baidu.com/images/weather/night/duoyun.png\"," + "\"weather\": \"阵雨转多云\"," + "\"wind\": \"西北风4-5级\"," + "\"temperature\": \"21 ~ 14℃\"" + "}" + "]" + "}" + "]" + "}
JsonObject object = new JsonParser().parse(jsonStr).getAsJsonObject(); JsonArray array = object.get("results").getAsJsonArray(); for(JsonElement element : array){ JsonObject jo = element.getAsJsonObject(); String currentCity = jo.get("currentCity").getAsString(); Log.d("mytest","currentCity=" + currentCity); JsonArray jsonArray = jo.get("weather_data").getAsJsonArray(); for(JsonElement type : jsonArray){ WeatherData weatherData = gson.fromJson(type, WeatherData.class); Log.d("mytest","weatherData=" + weatherData); } }
如果想使用new Gson().fromJson(Str,class) 一步到位,那么需要将上面的字符串映射成3个对象,具体的解析方法可以参见:
http://blog.csdn.net/wyb112233/article/details/48103525
再来看一个字符串:
[ { "is_online": 1, "user_name": "guanghui.li", "birthday": "2014-10-21", "sex": "MALE", "id_user": 100, "face_id": 0, "face_url": "3c_home/face_img/da15e418-9e97-4480-9a15-29068e319c56", "signature": "哈哈魔", "is_test": 0 }, { "is_online": 1, "user_name": "111", "birthday": "2015-09-07", "sex": "MALE", "id_user": 111, "email": "5313321750@qq.com", "nickname": "双方都", "face_id": 6, "face_url": "3c_home/face_img/2e9fb3b8-5eee-4a5f-9cb3-851fc411d50f", "signature": "加油✌\n635958464", "is_test": 0 }, { "is_online": 0, "user_name": "115", "birthday": "2020-09-22", "sex": "MALE", "id_user": 115, "nickname": "阿斯蒂芬...", "face_id": 8, "face_url": "3c_home/8b4cec54-6c70-4487-a32b-b4432f2e8185", "signature": "梦想还是要有的,万一实现了呢?", "is_test": 0 }, { "is_online": 0, "user_name": "zhipeng.hui", "birthday": "1990-05-16", "sex": "MALE", "id_user": 129, "email": "zhipe2sng.hui@cloudsoar.com", "nickname": "阿斯蒂芬", "face_id": 1, "face_url": "3c_home/face_img/ba2eca97-4577-427c-9a58-4a24126af824", "signature": "务实才能创新,创新才能发展。", "is_test": 0 }, { "is_online": 0, "user_name": "150135012275", "sex": "FEMALE", "id_user": 136, "nickname": "大后天", "face_id": 0, "face_url": "3c_home/face_img/91aa55ab-b60c-4b98-8939-345c0c34cd43", "signature": "", "is_test": 0, "mobile": "150131502275" }, { "is_online": 0, "user_name": "180381210025", "birthday": "1899-12-30", "sex": "FEMALE", "id_user": 138, "nickname": "棼棼", "face_id": 256, "face_url": "3c_home/face_img/85e54960-000e-4780-a440-ebb07f40be96", "signature": "心若向阳,何惧伤悲!", "is_test": 0, "mobile": "186651927005" }, { "is_online": 0, "user_name": "186887180051", "sex": "MALE", "id_user": 139, "nickname": "James", "face_id": 0, "face_url": "3c_home/face_img/ee701de0-1404-4d5f-b9bf-4da505284007", "is_test": 0, "mobile": "186887180051" }, { "is_online": 0, "user_name": "138128868458", "birthday": "2001-08-03", "sex": "MALE", "id_user": 168, "nickname": "看看看韩国", "face_id": 0, "face_url": "3c_home/face_img/c70e9cab-56a3-46e3-ab2d-61dfbca61f74", "is_test": 0, "mobile": "138288618458" }, { "is_online": 0, "user_name": "huo2", "birthday": "2010-02-26", "sex": "FEMALE", "id_user": 201, "nickname": "不良人", "face_id": 0, "face_url": "3c_home/face_img/5f9b4921-1abf-4a67-8810-b20024867911", "signature": "彪悍的人生不需要解释", "is_test": 0 }, { "is_online": 0, "user_name": "水电费", "birthday": "2015-01-16", "sex": "FEMALE", "id_user": 336, "nickname": "水电费", "face_id": 8, "face_url": "3c_home/face_img/7376c9c7-fbc1-4461-9444-0370f8e38ef0", "signature": "学如逆水行舟,不进则退", "is_test": 0 }, { "is_online": 1, "user_name": "186881889823", "sex": "", "id_user": 526, "nickname": "randy", "face_id": 0, "mobile": "186818889823" }, { "is_online": 0, "user_name": "阿萨德", "birthday": "1992-06-30", "sex": "MALE", "id_user": 666, "nickname": "阿萨德", "face_id": 0, "face_url": "3c_home/face_img/7b06eb1c-0da0-4eb7-972a-0311e1ec328f", "signature": "", "is_test": 0 }, { "is_online": 0, "user_name": "好", "birthday": "1988-04-26", "sex": "MALE", "id_user": 888, "email": "3801111720@qq.com", "nickname": "规范", "face_id": 0, "face_url": "3c_home/face_img/3ec8e894-f457-42b8-993f-ce010c4154b7", "signature": "", "is_test": 0 }, { "is_online": 1, "user_name": "阿斯蒂芬", "birthday": "1900-01-27", "sex": "MALE", "id_user": 889, "email": "xx@xx.com", "nickname": "阿斯蒂芬", "face_id": 0, "face_url": "3c_home/face_img/a43042f6-4b65-4ba9-91ee-716daa70a926", "signature": "", "is_test": 0 }, { "is_online": 1, "user_name": "ccccc", "birthday": "0001-01-01", "sex": "MALE", "id_user": 1122, "email": "5477171003@qq.com", "nickname": "cccc", "face_id": 0, "face_url": "3c_home/3667abde-d073-4a27-ab36-ec8a81a52d72", "signature": "" }, { "is_online": 0, "user_name": "159200016474", "birthday": "1989-11-20", "sex": "FEMALE", "id_user": 3480, "nickname": "Carol", "face_id": 0, "face_url": "3c_home/face_img/60ff35e1-9d74-4150-8802-3e35a304f1c3", "signature": "宠辱不惊,看庭前花开花落;去留无意,望天上云卷云舒。", "mobile": "159200061474" }, { "is_online": 0, "user_name": "131281758281", "birthday": "2015-06-02", "sex": "FEMALE", "id_user": 3773, "nickname": "sky", "face_id": 0, "face_url": "3c_home/face_img/7cac4b97-b902-490b-b34d-057ed5682226", "signature": "乐在勾通^_^", "mobile": "131128758281" }, { "is_online": 1, "user_name": "caoguigui", "birthday": "2015-08-11", "sex": "FEMALE", "id_user": 42052, "nickname": "暗红色的", "face_id": 8, "face_url": "3c_home/873a093d-c644-4ee1-b61a-237e191627c4", "signature": "go go go!" }, { "is_online": 0, "user_name": "136911887425", "birthday": "1985-11-11", "sex": "MALE", "id_user": 42188, "nickname": "按时到岗", "face_id": 0, "face_url": "3c_home/face_img/85f4ddf2-6185-4ed2-9fa0-8f16b19e6847", "signature": "加油。。。。", "mobile": "136918187425" }, { "is_online": 0, "user_name": "miaozi", "birthday": "2015-09-07", "sex": "FEMALE", "id_user": 42229, "nickname": "阿斯蒂芬", "face_id": 5, "face_url": "3c_home/7faec67c-52a5-40e3-9501-d88543850ab8", "signature": "空军建军节空军建军节空军建军节空军建军节空军建军节空军建军节空军建军节空军建军节空军建军节空军建军节空军建军节空军建军节空军建军节空军建军节空军建军节空军建军节空军建军节空军建军节空军建军节空军建军节空军建军节空军建军节空", "mobile": "185031008635" }, { "is_online": 0, "user_name": "Cgg", "birthday": "2015-10-13", "sex": "FEMALE", "id_user": 42262, "email": "cggc23zf@163.com", "nickname": "阿什顿发", "face_id": 7, "face_url": "3c_home/face_img/ef7da1fc-8bd4-460f-a26c-cb1a46982471", "signature": "⛪️" }, { "is_online": 0, "user_name": "koukou", "birthday": "1985-11-11", "sex": "FEMALE", "id_user": 42670, "email": "79144102359@qq.com", "nickname": "儿童", "face_id": 0, "face_url": "3c_home/face_img/c0b70bdd-5242-40f6-bbc6-726aeb0f9c57", "signature": "来咯哦哦五塔寺蘑菇街啦咯啦咯啦咯空军建军节空军建军节空军建军摸摸摸啊啊啊空军建军节空军建军节来的人的啦咯啦咯啦咯空军建军节空军建军节啦咯啦咯啦咯噢噢噢哦哦了考虑兔兔空军建军节啦咯啦咯啦咯空军建军节空军" }, { "is_online": 0, "user_name": "186658711161", "birthday": "1992-11-06", "sex": "MALE", "id_user": 42953, "email": "zengxi212angjian.cn@gmail.com", "nickname": "同意", "face_id": 0, "signature": "" }, { "is_online": 0, "user_name": "huo3", "birthday": "2015-11-30", "sex": "", "id_user": 42969, "nickname": "huo3", "face_id": 0, "face_url": "3c_home/face_img/4fce24e3-ac84-4e62-987f-583005c75fca", "signature": "..............", "mobile": "1110" }, { "is_online": 0, "user_name": "15019286469", "birthday": "1990-05-05", "sex": "FEMALE", "id_user": 42987, "nickname": "卷", "face_id": 0, "face_url": "3c_home/face_img/e591f9dc-3740-4874-916b-a1b72a4d162f", "signature": "时光海苔。", "mobile": "150192861469" }, { "is_online": 0, "user_name": "eric", "birthday": "2015-06-29", "sex": "MALE", "id_user": 43515, "nickname": "UI水电费", "face_id": 0, "face_url": "3c_home/face_img/abf86610-5623-4971-85ba-32a17f9a21ae", "signature": " ", "mobile": "186030715850" }, { "is_online": 0, "sex": "", "id_user": 43664, "nickname": "加油冲刺", "face_id": 0 }, { "is_online": 0, "birthday": "2015-10-16", "sex": "FEMALE", "id_user": 43666, "nickname": "感觉萌萌哒", "face_id": 0, "face_url": "3c_home/face_img/bc7d9376-f323-4e83-8f82-b1e6ee3ad826", "signature": "因为爱情 ……" }, { "is_online": 1, "user_name": "wangyb", "birthday": "1986-11-10", "sex": "MALE", "id_user": 43905, "email": "3951044952@qq.com", "nickname": "问问", "face_id": 0, "face_url": "3c_home/9245335e-b8f1-43f5-b023-eb933b061bd2", "signature": "", "mobile": "186892010174" }, { "is_online": 0, "user_name": "Mona", "birthday": "1905-11-09", "sex": "FEMALE", "id_user": 43980, "nickname": "Mona", "face_id": 0, "face_url": "3c_home/face_img/094a7a2f-bfd3-491e-bb77-e3edd2c080bf", "signature": "", "mobile": "1867588171506" }, { "is_online": 0, "user_name": "阿什顿发", "birthday": "1991-01-12", "sex": "MALE", "id_user": 666668, "email": "junbin.liu@cloudsoar.com", "nickname": "地方噶", "face_id": 0, "face_url": "3c_home/face_img/c792406c-90dd-4c2b-8628-4615901749e4", "signature": "✌", "is_test": 0 }, { "is_online": 0, "user_name": "cheng.li", "birthday": "1988-04-26", "sex": "MALE", "id_user": 888888, "email": "2607443042@qq.com", "nickname": "阿什顿发", "face_id": 0, "face_url": "3c_home/face_img/68a3bea9-6349-4f4d-b7e3-d05a6af015aa", "signature": "", "is_test": 0, "mobile": "cheng.li" }, { "is_online": 0, "user_name": "wengcx88", "birthday": "1985-11-11", "sex": "MALE", "id_user": 1001019, "email": "wengcx520@sina.com", "nickname": "酷狗", "face_id": 9, "face_url": "", "signature": "寇可往,吾亦可往!", "is_test": 0 }, { "is_online": 0, "user_name": "junsong.huang", "birthday": "1915-12-28", "sex": "MALE", "id_user": 1234658, "nickname": "驱蚊贴", "face_id": 2, "face_url": "3c_home/face_img/d2dd20ef-5b2f-458e-8000-efdcd6c269be", "signature": "Tyrion Lannister", "is_test": 0 }]
Gson gson = new Gson();Type type = new TypeToken<List<SecretContacts>>() {}.getType();List<SecretContacts> contacts = gson.fromJson(json, type);
使用上面的TypeToken就可以一步将字符串转为集合,具体TypeToken的介绍和用法,请参见:
http://blog.csdn.net/zzp_403184692/article/details/8266575
好了,其实碰到json格式的字符串做解析,关键看需求是什么,哪些字段需要单独解析出来,哪部分需要作为一个对象解析出来。关键是根据需求做分析,然后再动手处理。
- 使用Gson解析json格式的字符串的正确方式
- 使用Gson解析json格式的字符串的正确方式
- gson 的使用 解析json字符串
- 使用GSON和泛型解析约定格式的JSON字符串
- 使用库org.json 和 Gson 解析 JSON格式字符串
- Retrofit中使用GSON解析定制格式的JSON
- JSON数据的格式和解析(使用GSON)入门
- JSON格式数据解析及GSON框架的使用详解
- Gson 解析json的使用
- JSON解析 Gson的使用
- 使用Gson解析键名为中文的json字符串
- 工作笔记之复杂字符串的json解析,使用gson
- 用Gson解析复杂的Json字符串
- 解决android使用gson解析json字符串,并使用混淆编译方式打包apk遇到的问题
- 解决android使用gson解析json字符串,并使用混淆编译方式打包apk遇到的问题
- 解决android使用gson解析json字符串,并使用混淆编译方式打包apk遇到的问题
- 使用Gson解析复杂的json数据
- 使用Gson解析复杂的json数据
- c++单独编译
- Windows程序设计(Hello,word!)
- project euler 29
- 印度足球之最
- project euler 30
- 使用Gson解析json格式的字符串的正确方式
- struts2必备包
- Android自定义组件之圆形图片
- 过滤器及监听器
- 38.XML数据解析的SAX解析
- Windows程序设计(与窗口的第一次)
- char *s 与 char s[]的区别
- LightOJ 1140How Many Zeroes?
- 异步FIFO设计原理及Verliog源代码