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的更多相关文章

  1. android解析json

    android2.3提供的json解析类 android的json解析部分都在包org.json下,主要有以下几个类: JSONObject:可以看作是一个json对象 JSONStringer:js ...

  2. 第十七章:android解析JSON

    一.解析JSON数据: 首先引入包import org.json.JSONObject;(android sdk 14以后应该自带了 ) Android端的程序解析JSON和JSON数组: packa ...

  3. Android解析Json速度最快的库:json-smart

    场景描写叙述: 本文仅验证了在安卓环境下使用Json的Key作为反序列化条件的解析速度.结论是解析速度最快的不是阿里的fastjson,也不是Google的Gson,而是json-smart. And ...

  4. Android解析Json数据之Gson解析

    Gson是谷歌官方提供的解析json数据的工具类.json数据的解析能够使用JSONObject和JSONArray配合使用解析数据,可是这样的原始的方法对于小数据的解析还是有作用的,可是陪到了复杂数 ...

  5. Android 解析JSON

    上次讲了XML格式数据的解析方式,这次要说的是如何解析JSON数据格式,相对与XML,JSON解析数据的方式在于它的体积更小,在网络上传输可以更省流量. 这次在网上找到一个中国天气json数据的API ...

  6. Android 解析JSON格式数据

    比起XML,JSON主要优势在于它的体积更小,在网络上传输的时候可以更省流量.但缺点在于,它的语义性较差,显示不如XML直观. JSON格式 :  { "name_A" : &qu ...

  7. Android解析json数据

    Json数据 [{"code":"110000","sheng":"11","di":"0 ...

  8. [转]Android解析json数据

    1.json格式 2.json解析 3.gson解析 4.fastjson解析 一.Json格式 json一种轻量级的数据交换格式.在网络上传输交换数据一般用xml, json. 两种结构: 1)对象 ...

  9. android解析json包(接口)

    package com.http.test; 02    03    04 import org.apache.http.HttpResponse; 05 import org.apache.http ...

随机推荐

  1. Flex 动画效果

    1.使用自带效果 在Flex里面不像在Flash里面随意制作动画了,Flex更趋向于应用程序,而不是动画制作了,所以没有了时间轴的概念.在Flex中使用动画效果,可以用Flex自带的Effect,或者 ...

  2. vim: 字符串替换

    :s/str1/str2/ 替换当前行第一个 str1 为 str2:s/str1/str2/g 替换当前行所有 str1 为 str2 ( 注意,  s/xx/xxx/g 语句从s开始,中间的空格视 ...

  3. C++之类的静态变量

    成员变量 通过对象名能够访问public成员变量 每个对象都可以有只属于自己的成员变量 成员变量不能在对象之间共享 类的静态成员 静态成员变量  存储在   全局数据区 #include<std ...

  4. 模仿jquery的一些实现 第二版

    具体如下: //w作为window的形参,就表示window (function(w) { // 定义一个全局的window.wyl变量,就类似于jquery里的$,Jquery对象 w.wyl; / ...

  5. Clob对象转为字符串

    项目中遇到一个问题,对方公司把打印好的报表数据存到数据库中,实际上就是把html存在Oracle中,然后需要我们在社保系统里进行查询. 但是他们把数据存放在B数据库,而我们的社保系统用的数据库是B.A ...

  6. Android——用户登陆及用户名和密码的保存

    Android——用户登陆及用户名和密码的保存   在之前的学习过程中已经将Android学习完了,但是在后面将近一年的时间里都没有进行过Android开发,所以对Android的所有的知识点又有点忘 ...

  7. PHP 时间和日期 总结

    PHP 时间戳 UNIX 时间戳(timestamp)是 PHP 中关于时间日期一个很重要的概念,它表示从 1970年1月1日 00:00:00 到当前时间的秒数之和. 可以使用time()函数来获取 ...

  8. XIV

    http://publib.boulder.ibm.com/infocenter/ibmxiv/r2/index.jsp

  9. js 选择器

    a>b  获取a下面的直接子元素

  10. Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer'

    @Configuration public class HttpSessionConfig { @Bean public static ConfigureRedisAction configureRe ...