加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

JsonReader JSONObject 区别

发布时间:2020-12-16 19:19:20 所属栏目:百科 来源:网络整理
导读:由于使用了GSON库解析json数据成不同的类。然后就找到了两种方法。不懂那种效率高。有时间还请高人指教。先上代码先。 首先是jsonreader的: JsonReader reader = new JsonReader(new StringReader(content)); reader.beginObject(); String tagName = reade

由于使用了GSON库解析json数据成不同的类。然后就找到了两种方法。不懂那种效率高。有时间还请高人指教。先上代码先。

首先是jsonreader的:

JsonReader reader = new JsonReader(new StringReader(content));
reader.beginObject();
String tagName = reader.nextName();
if (tagName.equals("response_params")) {
while (reader.hasNext()) {
String tag = reader.nextName();
if (tag.equals("appid")) {
appid = reader.nextString();
}else if (tag.equals("channel_id")) {
channelid = reader.nextString();
}else if (tag.equals("user_id")) {
userid = reader.nextString();
} else {
reader.skipValue();//忽略值/跳过break
}
}
}
reader.endObject();

然后是JSONObject 的:

JSONObject jsonContent = new JSONObject(content);
JSONObject params = jsonContent
.getJSONObject("response_params");
appid = params.getString("appid");
channelid = params.getString("channel_id");
userid = params.getString("user_id");


一眼看过去。明显jsonobject简单很多。就是不知道效率怎么样。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读