在android解析json
1、采用一般方式解释json为对象
package com.heimazyh.testjson; import org.json.JSONException;
import org.json.JSONObject; import com.heimazyh.http.Request;
import com.heimazyh.http.Response; public class DoJson implements Response { @Override
public void process(Object object) {
// TODO Auto-generated method stub
if(object != null){
String str = (String) object;
try {
JSONObject jsonObject = new JSONObject(str);
int id = jsonObject.getInt("id");
System.out.println("id=" + id); String name = jsonObject.getString("name");
System.out.println("name=" + name); String address = jsonObject.getString("address");
System.out.println("address=" + address);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} } @Override
public Request getRequest() {
// TODO Auto-generated method stub
String path = "http://192.168.1.101/android/json/echojson.php";
String method="get";
Request request = new Request(path,method);
return request;
} }
2、采用一般方式解析json为List
package com.heimazyh.testjson; import java.util.ArrayList;
import java.util.List; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import com.heimazyh.domain.Person;
import com.heimazyh.http.Request;
import com.heimazyh.http.Response; public class PersonsJson implements Response { @Override
public void process(Object object) {
// TODO Auto-generated method stub
if(object != null){
String str = (String) object;
try {
List<Person> list = new ArrayList<Person>();
JSONArray jsonArray = new JSONArray(str);
for(int index=0; index < jsonArray.length(); index++){
JSONObject jsonObject = jsonArray.getJSONObject(index);
Person person = new Person();
person.setId(jsonObject.getInt("id"));
person.setName(jsonObject.getString("name"));
person.setAddress(jsonObject.getString("address"));
list.add(person);
} for(int i=0; i < list.size(); i++){
Person person2 = list.get(i);
System.out.println(person2.toString());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} @Override
public Request getRequest() {
// TODO Auto-generated method stub
String path = "http://192.168.1.101/android/json/persons.php";
String method="get";
Request request = new Request(path,method);
return request;
} }
3、采用一般方式解析json为map
package com.heimazyh.testjson; import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map; import org.json.JSONArray;
import org.json.JSONObject; import android.util.Log; import com.heimazyh.http.Request;
import com.heimazyh.http.Response; public class MapJson implements Response { @Override
public void process(Object object) {
// TODO Auto-generated method stub
if(object != null){
String str = (String) object;
try{
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
JSONObject jsonObject = new JSONObject(str);
JSONArray jsonArray = jsonObject.getJSONArray("persons");
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
Map<String, Object> map = new HashMap<String, Object>();
Iterator<String> iterator = jsonObject2.keys();
while(iterator.hasNext()){
String key = iterator.next();
Object value = jsonObject2.get(key);
if(key == null){
key = "";
}
map.put(key, value);
}
list.add(map);
} Log.i("maptest", list.toString());
}catch(Exception e){
e.printStackTrace();
}
} } @Override
public Request getRequest() {
// TODO Auto-generated method stub
String path = "http://192.168.1.101/android/json/echomap.php";
String method="get";
Request request = new Request(path,method);
return request;
} }
4、使用gson进行解析
package com.heimazyh.testjson; import com.google.gson.Gson;
import com.heimazyh.domain.Person;
import com.heimazyh.http.Request;
import com.heimazyh.http.Response; public class GsonForPerson implements Response { @Override
public void process(Object object) {
// TODO Auto-generated method stub
if(object != null){
String str = (String) object;
Gson gson = new Gson();
Person person = gson.fromJson(str, Person.class);
System.out.println(person.toString());
System.out.println(person.getId());
System.out.println(person.getName());
System.out.println(person.getAddress());
} } @Override
public Request getRequest() {
// TODO Auto-generated method stub
String path = "http://192.168.1.100/android/json/echojson.php";
String method="get";
Request request = new Request(path,method);
return request;
} }
package com.heimazyh.testjson; import java.lang.reflect.Type;
import java.util.List; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.heimazyh.domain.Person;
import com.heimazyh.http.Request;
import com.heimazyh.http.Response; public class GsonForPersons implements Response { @Override
public void process(Object object) {
// TODO Auto-generated method stub
if(object != null){
String str = (String) object;
Gson gson = new Gson();
Type type = new TypeToken<List<Person>>(){}.getType();
List<Person> persons = gson.fromJson(str, type);
for(Person person : persons){
System.out.println(person.toString());
System.out.println(person.getId());
System.out.println(person.getName());
System.out.println(person.getAddress());
}
}
} @Override
public Request getRequest() {
// TODO Auto-generated method stub
String path = "http://192.168.1.100/android/json/persons.php";
String method="get";
Request request = new Request(path,method);
return request;
} }
在android解析json的更多相关文章
- android解析json
android2.3提供的json解析类 android的json解析部分都在包org.json下,主要有以下几个类: JSONObject:可以看作是一个json对象 JSONStringer:js ...
- 第十七章:android解析JSON
一.解析JSON数据: 首先引入包import org.json.JSONObject;(android sdk 14以后应该自带了 ) Android端的程序解析JSON和JSON数组: packa ...
- Android解析Json速度最快的库:json-smart
场景描写叙述: 本文仅验证了在安卓环境下使用Json的Key作为反序列化条件的解析速度.结论是解析速度最快的不是阿里的fastjson,也不是Google的Gson,而是json-smart. And ...
- Android解析Json数据之Gson解析
Gson是谷歌官方提供的解析json数据的工具类.json数据的解析能够使用JSONObject和JSONArray配合使用解析数据,可是这样的原始的方法对于小数据的解析还是有作用的,可是陪到了复杂数 ...
- Android 解析JSON
上次讲了XML格式数据的解析方式,这次要说的是如何解析JSON数据格式,相对与XML,JSON解析数据的方式在于它的体积更小,在网络上传输可以更省流量. 这次在网上找到一个中国天气json数据的API ...
- Android 解析JSON格式数据
比起XML,JSON主要优势在于它的体积更小,在网络上传输的时候可以更省流量.但缺点在于,它的语义性较差,显示不如XML直观. JSON格式 : { "name_A" : &qu ...
- Android解析json数据
Json数据 [{"code":"110000","sheng":"11","di":"0 ...
- [转]Android解析json数据
1.json格式 2.json解析 3.gson解析 4.fastjson解析 一.Json格式 json一种轻量级的数据交换格式.在网络上传输交换数据一般用xml, json. 两种结构: 1)对象 ...
- android解析json包(接口)
package com.http.test; 02 03 04 import org.apache.http.HttpResponse; 05 import org.apache.http ...
随机推荐
- Tree(未解决。。。)
Tree Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- Django的TemplateResponse
def my_render_callback(response): return response from django.template.response import TemplateRespo ...
- 深入理解java嵌套类和内部类
一.什么是嵌套类及内部类 能够在一个类的内部定义还有一个类.这样的类称为嵌套类(nested classes),它有两种类型:静态嵌套类和非静态嵌套类.静态嵌套类使用非常少,最重要的是非静态嵌套类,也 ...
- String、StringBuffer、StringBuilder之间区别
String,StringBuffer,StringBuilder 之间区别 在字符串处理中C#提供了String.StringBuffer.StringBuilder三个类.那么他们到底有什么优缺点 ...
- (译)Node.js的全局变量
原文标题:Global Variables in Node.js 原文链接:http://www.hacksparrow.com/global-variables-in-node-js.html 你可 ...
- :before :after
#p1:before{ content: "哈哈哈 "; color: red;}#p1:after{ content: "哈哈哈"; color: #452d ...
- Sharepoint 2013 启用搜做服务
参考文件: http://www.cnblogs.com/jianyus/archive/2013/02/04/2891801.html 1. 创建好网站集,进入网站内容,点击搜素,会出现如下错误:( ...
- 循环训练(for的嵌套、while、do while)以及异常处理
For的嵌套 练习一: 练习二: 练习三: 练习四: while的使用方法: 示例一: 示例二: 示例三: while的练习题: do while的使用示例: 异常处理示例: try catch ...
- Enze fourth day(循环语句 一)
哈喽,大家好.又到了总结知识的时间了.今天在云和学院自学了一下循环语句,下面是自己总的一些知识点. 先补充一下选择结构中的switch语句. 理论:switch语句是一种多分支选择语句,当需要测试大量 ...
- linux驱动调试--段错误之oops信息分析
linux驱动调试--段错误之oops信息分析 http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=29401328&id= ...