美文网首页
google.Gson解析jsonStr

google.Gson解析jsonStr

作者: 游园拾忆 | 来源:发表于2017-12-07 14:34 被阅读12次
    package com.mandi.system.test;
    
    import com.google.gson.JsonArray;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonParser;
    
    public class zjwTest {
        public static void main(String[] args) {
            String jsonStr = "{" + "'title':'get_msg_setting_info',"
                    + "'groupList':[{'ses_id':'F9E0147B-3444-413F-A568-C2D49464CCFB'},{'ses_id': '2b694b46-b839-4364-905f-b352651c53a9'}],"
                    + "'my_ip':'112.95.153.98'" + "}";
    
            JsonObject jObject = new JsonParser().parse(jsonStr).getAsJsonObject();
            // title : 字符串
            String title = jObject.get("title").getAsString();
            System.out.println("title:" + title);
    
            // groupList : List集合
            JsonArray array = jObject.get("groupList").getAsJsonArray();
            for (JsonElement jsonElement : array) {
                JsonObject jo = jsonElement.getAsJsonObject();
                String ses_id = jo.get("ses_id").getAsString();
    
                System.out.println("ses_id:" + ses_id);
            }
    
            // my_ip : 字符串
            String my_ip = jObject.get("my_ip").getAsString();
            System.out.println("my_ip:" + my_ip);
    
        }
    }
    

    相关文章

      网友评论

          本文标题:google.Gson解析jsonStr

          本文链接:https://www.haomeiwen.com/subject/gjhxmxtx.html