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. 工具篇-MAT(Memory Analyzer Tool)

    --- layout: post title: 工具篇-MAT(Memory Analyzer Tool) description: 让内存泄漏无所遁形 2015-10-08 category: bl ...

  2. OpenCV 安装

    OpenCV 安装 一.环境说明: 操作系统:window10 opencv版本是:VERSION3.1 二.安装过程: [1]官网下载:http://opencv.org/downloads.htm ...

  3. CoffeeScript 入门笔记

    写在前面: 被英文版指南坑了...闹了很久才明白.coffee怎么用.安装前需要有稳定版 Node.js, 和 npm (Node Package Manager). 借助 npm 可以安装 Coff ...

  4. .net运行时和核心类库源码(部分源码)微软官方下载

    部分类库代码:http://referencesource.microsoft.com/download.html 运行时clr源码: http://www.microsoft.com/en-us/d ...

  5. Qt之QNetworkInterface(查询网络接口),QHostInfo(查询主机IP)

    http://blog.csdn.net/u011012932/article/details/50775052 http://blog.csdn.net/u011012932/article/det ...

  6. c++ 复制构造函数和赋值函数

    c++ 自动提供了下面这些成员函数 1默认构造函数 2.复制构造函数 3.赋值操作符 4.默认析构函数 5.地址操作符 赋值构造函数copy construtor 用于将一个对象复制到新创建的对象中, ...

  7. 雪佛兰Suburban_百度百科

    雪佛兰Suburban_百度百科 雪佛兰Suburban

  8. 如何提高banner设计含量--网上的一篇文章--感悟

    "修改": 本质上是改什么?改大小?图片?文字?颜色? 老板说:修改本质上是提高“设计含量”.检测一个作品设计含量的高低,可以将作品中每一个设计元素进行分析,看它的“属性”与“操作 ...

  9. UIColor深入研究(CGColor,CIColor)

    由于跟人比较喜欢研究关于图层与动画方面的技术,正打算看看别人写的好东西,就遇到了好几个问题, 第一:UIClor类方法的使用 就是关于UIColor的使用,记得之前开发中我们使用的都是UIColor后 ...

  10. (void)(&amp;x==&amp;y)

    #define max(x,y) ({ \ typeof(x) _x = (x);    \ typeof(y) _y = (y);    \ (void) (&_x == &_y); ...