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. hdoj 5311 Hidden String(KMP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5311 思路分析:该问题要求在字符串中是否存在三个不相交的子串s[l1..r1], s[l2..r2], ...

  2. pyrailgun 0.24 : Python Package Index

    pyrailgun 0.24 : Python Package Index pyrailgun 0.24 Download pyrailgun-0.24.zip Fast Crawler For Py ...

  3. 测试横竖屏切换时activity 的生命周期

    对于这个面试题,相信大家都见过,网上给出的答案是: 1.不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次 2.设 ...

  4. iOS开发之第三方登录QQ -- 史上最全最新第三方登录QQ方式实现

    项目地址 :  https://github.com/zhonggaorong/QQLoginDemo/tree/master 最新版本的qq登录实现步骤实现: 1. 首先,你需要去向腾讯申请账号. ...

  5. Spring源码地址和相关介绍的网址

    Spring源码地址下载: https://github.com/spring-projects/spring-framework/tags >多图详解Spring框架的设计理念与设计模式:ht ...

  6. C++对C语言的非面向对象特性扩充(1)

    我将分3篇来介绍C++相对于C在非对象特性上的扩充,今天要讲的是C++在注释,输入输出,局部变量说明的扩充,以及const修饰符与C中的#define的比较. 1.C++注释除了包括原有C的块注释/* ...

  7. BZOJ 1617: [Usaco2008 Mar]River Crossing渡河问题( dp )

    dp[ i ] = max( dp[ j ] + sum( M_1 ~ M_( i - j ) ) + M , sum( M_1 ~ M_i ) ) ( 1 <= j < i )  表示运 ...

  8. Java Web学习笔记(1)

    1.项目名称用小写,类名用大小写骆驼式,对象名用骆驼式但是第一个字母是小写: 2.写对象属性时要空行,第一个方法也要空行,一般要加注释: 3.new 新的对象时等号左右要空格,if语句左右摇有空格: ...

  9. Log Collect

    http://ossectools.blogspot.com/2011/03/comprehensive-log-collection.html https://www.hacking-lab.com ...

  10. 第四节 Code 39 码 / 三九码

    39码是西元1974年发展出来的条码系统,是一种可供使用者双向扫瞄的分散式条码,也就是说相临两资料码之间,必须包含一个不具任何意义的空白(或细白,其逻辑值为0),且其具有支援文数字的能力,故应用较一般 ...