星空网 > 软件开发 > Java

Gson解析JsonObject和JsonArray

Gson中重要的几个核心类:

Gson、JsonParser、JsonObject、JsonArray。

下面就是解析的步骤:

public void parserJsonArray(String strJson){
//创建一个Gson对象
Gson gson = new Gson();
//创建一个JsonParser
JsonParser parser = new JsonParser();
//通过JsonParser对象可以把json格式的字符串解析成一个JsonElement对象
JsonElement el = parser.parse(strJson);

//把JsonElement对象转换成JsonObject
JsonObject jsonObj = null;
if(el.isJsonObject()){
jsonObj = el.getAsJsonObject();
}


//把JsonElement对象转换成JsonArray
JsonArray jsonArray = null;
if(el.isJsonArray()){
jsonArray = el.getAsJsonArray();
}

//遍历JsonArray对象
Iterator it = jsonArray.iterator();
while(it.hasNext()){
JsonElement e = (JsonElement)it.next();
//JsonElement转换为JavaBean对象
jbDemo= gson.fromJson(e, JavaBeanDemo.class);
}

}



原标题:Gson解析JsonObject和JsonArray

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

怎样做好亚马逊站内CPC广告?:https://www.ikjzd.com/articles/128117
旺季硝烟起!环球易购要申请2亿授信!:https://www.ikjzd.com/articles/128120
TikTok收购大乱斗,特朗普头号支持者下场:https://www.ikjzd.com/articles/128125
亚马逊Rview与销量比多少正常?:https://www.ikjzd.com/articles/128134
怎么撰写/优化亚马逊listing?(纯干货):https://www.ikjzd.com/articles/128135
产品出口美国有哪些认证:https://www.ikjzd.com/articles/128144
延安市区景点都收费吗 延安景点要门票吗:https://www.vstour.cn/a/408250.html
大同旅游攻略一日游 山西大同一日游旅游景点有哪些:https://www.vstour.cn/a/408251.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流