Android中的JSONObject和JSONArray解析json数据_PHP教程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android中的JSONObject和JSONArray解析json数据_PHP教程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1699字,纯文字阅读大概需要3分钟。
内容图文
今天介绍一下关于json数据解析,我们使用Android中的JSONObject和JSONArray解析json数据,有android开发的朋友可以参考一下。 String strJson = "{"students":[{"name":"Jack","age":12}, {"name":"Vista","age":23}, {"name":"Kaka","age":22}, {"name":"Hony","age":31}]}";
try {
JSONObject jo = new JSONObject(strJson);
JSONArray jsonArray = (JSONArray) jo.get("students");
for (int i = 0; i < jsonArray.length(); ++i) {
JSONObject o = (JSONObject) jsonArray.get(i);
System.out.println("name:" + o.getString("name") + "," + "age:"
+ o.getInt("age"));
}
} catch (JSONException e) {
e.printStackTrace();
}
2.使用gson中的JsonReader解析json数据
try {
String string = "{"class":1, "students":[{"name":"jack", "age":21},{"name":"kaka", "age":21},{"name":"lucy", "age":21}]}";
StringReader sr = new StringReader(string);
JsonReader jr = new JsonReader(sr);
jr.beginObject();
if (jr.nextName().contains("class")) {
System.out.println("班级: " + jr.nextString());
if (jr.nextName().equals("students")) {
jr.beginArray();
while (jr.hasNext()) {
jr.beginObject();
if (jr.nextName().equals("name"))
System.out.print("姓名:" + jr.nextString());
if (jr.nextName().equals("age")) {
System.out.println(" , 年龄:" + jr.nextInt());
}
jr.endObject();
}
jr.endArray();
}
}
jr.endObject();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Json解析库gson: http://code.google.com/p/google-gson/
http://www.bkjia.com/PHPjc/630065.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630065.htmlTechArticle今天介绍一下关于json数据解析,我们使用Android中的JSONObject和JSONArray解析json数据,有android开发的朋友可以参考一下。 String strJson = {students:...
内容总结
以上是互联网集市为您收集整理的Android中的JSONObject和JSONArray解析json数据_PHP教程全部内容,希望文章能够帮你解决Android中的JSONObject和JSONArray解析json数据_PHP教程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。