在android解析json

1、采用一般方式解释json为对象

 1 package com.heimazyh.testjson;
 2
 3 import org.json.JSONException;
 4 import org.json.JSONObject;
 5
 6 import com.heimazyh.http.Request;
 7 import com.heimazyh.http.Response;
 8
 9 public class DoJson implements Response {
10
11     @Override
12     public void process(Object object) {
13         // TODO Auto-generated method stub
14         if(object != null){
15             String str = (String) object;
16             try {
17                 JSONObject jsonObject = new JSONObject(str);
18                 int id = jsonObject.getInt("id");
19                 System.out.println("id=" + id);
20
21                 String name = jsonObject.getString("name");
22                 System.out.println("name=" + name);
23
24                 String address = jsonObject.getString("address");
25                 System.out.println("address=" + address);
26             } catch (JSONException e) {
27                 // TODO Auto-generated catch block
28                 e.printStackTrace();
29             }
30         }
31
32     }
33
34     @Override
35     public Request getRequest() {
36         // TODO Auto-generated method stub
37         String path = "http://192.168.1.101/android/json/echojson.php";
38         String method="get";
39         Request request = new Request(path,method);
40         return request;
41     }
42
43 }

2、采用一般方式解析json为List

 1 package com.heimazyh.testjson;
 2
 3 import java.util.ArrayList;
 4 import java.util.List;
 5
 6 import org.json.JSONArray;
 7 import org.json.JSONException;
 8 import org.json.JSONObject;
 9
10 import com.heimazyh.domain.Person;
11 import com.heimazyh.http.Request;
12 import com.heimazyh.http.Response;
13
14 public class PersonsJson implements Response {
15
16     @Override
17     public void process(Object object) {
18         // TODO Auto-generated method stub
19         if(object != null){
20             String str = (String) object;
21             try {
22                 List<Person> list = new ArrayList<Person>();
23                 JSONArray jsonArray = new JSONArray(str);
24                 for(int index=0; index < jsonArray.length(); index++){
25                     JSONObject jsonObject = jsonArray.getJSONObject(index);
26                     Person person = new Person();
27                     person.setId(jsonObject.getInt("id"));
28                     person.setName(jsonObject.getString("name"));
29                     person.setAddress(jsonObject.getString("address"));
30                     list.add(person);
31                 }
32
33                 for(int i=0; i < list.size(); i++){
34                     Person person2 = list.get(i);
35                     System.out.println(person2.toString());
36                 }
37             } catch (Exception e) {
38                 // TODO Auto-generated catch block
39                 e.printStackTrace();
40             }
41         }
42     }
43
44     @Override
45     public Request getRequest() {
46         // TODO Auto-generated method stub
47         String path = "http://192.168.1.101/android/json/persons.php";
48         String method="get";
49         Request request = new Request(path,method);
50         return request;
51     }
52
53 }

3、采用一般方式解析json为map

 1 package com.heimazyh.testjson;
 2
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.Iterator;
 6 import java.util.List;
 7 import java.util.Map;
 8
 9 import org.json.JSONArray;
10 import org.json.JSONObject;
11
12 import android.util.Log;
13
14 import com.heimazyh.http.Request;
15 import com.heimazyh.http.Response;
16
17 public class MapJson implements Response {
18
19     @Override
20     public void process(Object object) {
21         // TODO Auto-generated method stub
22         if(object != null){
23             String str = (String) object;
24             try{
25                 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
26                 JSONObject jsonObject = new JSONObject(str);
27                 JSONArray jsonArray = jsonObject.getJSONArray("persons");
28                 for(int i=0; i < jsonArray.length(); i++){
29                     JSONObject jsonObject2 = jsonArray.getJSONObject(i);
30                     Map<String, Object> map = new HashMap<String, Object>();
31                     Iterator<String> iterator = jsonObject2.keys();
32                     while(iterator.hasNext()){
33                         String key = iterator.next();
34                         Object value = jsonObject2.get(key);
35                         if(key == null){
36                             key = "";
37                         }
38                         map.put(key, value);
39                     }
40                     list.add(map);
41                 }
42
43                 Log.i("maptest", list.toString());
44             }catch(Exception e){
45                 e.printStackTrace();
46             }
47         }
48
49     }
50
51     @Override
52     public Request getRequest() {
53         // TODO Auto-generated method stub
54         String path = "http://192.168.1.101/android/json/echomap.php";
55         String method="get";
56         Request request = new Request(path,method);
57         return request;
58     }
59
60 }

4、使用gson进行解析

 1 package com.heimazyh.testjson;
 2
 3 import com.google.gson.Gson;
 4 import com.heimazyh.domain.Person;
 5 import com.heimazyh.http.Request;
 6 import com.heimazyh.http.Response;
 7
 8 public class GsonForPerson implements Response {
 9
10     @Override
11     public void process(Object object) {
12         // TODO Auto-generated method stub
13         if(object != null){
14             String str = (String) object;
15             Gson gson = new Gson();
16             Person person = gson.fromJson(str, Person.class);
17             System.out.println(person.toString());
18             System.out.println(person.getId());
19             System.out.println(person.getName());
20             System.out.println(person.getAddress());
21         }
22
23     }
24
25     @Override
26     public Request getRequest() {
27         // TODO Auto-generated method stub
28         String path = "http://192.168.1.100/android/json/echojson.php";
29         String method="get";
30         Request request = new Request(path,method);
31         return request;
32     }
33
34 }
 1 package com.heimazyh.testjson;
 2
 3 import java.lang.reflect.Type;
 4 import java.util.List;
 5
 6 import com.google.gson.Gson;
 7 import com.google.gson.reflect.TypeToken;
 8 import com.heimazyh.domain.Person;
 9 import com.heimazyh.http.Request;
10 import com.heimazyh.http.Response;
11
12 public class GsonForPersons implements Response {
13
14     @Override
15     public void process(Object object) {
16         // TODO Auto-generated method stub
17         if(object != null){
18             String str = (String) object;
19             Gson gson = new Gson();
20             Type type = new TypeToken<List<Person>>(){}.getType();
21             List<Person> persons = gson.fromJson(str, type);
22             for(Person person : persons){
23                 System.out.println(person.toString());
24                 System.out.println(person.getId());
25                 System.out.println(person.getName());
26                 System.out.println(person.getAddress());
27             }
28         }
29     }
30
31     @Override
32     public Request getRequest() {
33         // TODO Auto-generated method stub
34         String path = "http://192.168.1.100/android/json/persons.php";
35         String method="get";
36         Request request = new Request(path,method);
37         return request;
38     }
39
40 }
时间: 2024-07-29 17:26:10

在android解析json的相关文章

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

场景描写叙述: 本文仅验证了在安卓环境下使用Json的Key作为反序列化条件的解析速度.结论是解析速度最快的不是阿里的fastjson,也不是Google的Gson,而是json-smart. Android 4.4.2 fastjson-1.1.34.android.jar gson-2.2.4.jar json-smart-2.0-RC3.jar **注意场景的限定条件** 核心代码: package com.h3c.mytestview; import java.io.StringRead

Android 解析json数据

1.JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等).这些特性使JSON成为理想的数据交换语言. 易于人阅读和编写,同时也易于机器解析和生成(网络传输速度

android解析JSON

一.解析JSON数据: 首先引入包import org.json.JSONObject;(android sdk 14以后应该自带了 ) JSON数据如:String strJson = {"sid":"2","name":"张三"}; Android端的程序处理解析: JSONObject userJObject = new JSONObject(strJson ); String name = userObject.get

Android 解析JSON格式数据

比起XML,JSON主要优势在于它的体积更小,在网络上传输的时候可以更省流量.但缺点在于,它的语义性较差,显示不如XML直观. JSON格式 :  { "name_A" : "value_A","name_B" : "value_B" } 表示:name_A = value_A;  name_B = value_B; 我将对下面的JSON数据进行解析: [{"id":"5","

【ListViewJson】【MainActivity功能性分析,不讨论具体工具类的实现】【android解析json数据(包含对图片数据的异步缓存处理),并将其加载到listview中】

最近遇到了一个相当好.对初学者相当友善的项目,是描述如何将json数据解析,并加载到listview中. 但是个人认为以后所有类似功能的实现都可以使用这套工具. 项目也可以使用这套架构. 这个项目是处理每个news只有一个imgurl,应该考虑一下当imgurl数量不定的时候具体应该怎么解决. 首先项目源码结构如下: 项目下载链接:http://download.csdn.net/download/y562810463/8004245 在这个项目中的com.demo.app.common包完全可

Android解析JSON速度对比

转载参考:http://blog.csdn.net/h3c4lenovo/article/details/26568531 1 { 2 "testStr":"这是String的测试", 3 "testInt":12443, 4 "data": [ 5 { 6 "children": [ 7 { 8 "id": 10007, 9 "title": "北京&qu

android 解析json数据格式(转)

json数据格式解析我自己分为两种: 一种是普通的,一种是带有数组形式的: 普通形式的:服务器端返回的json数据格式如下: {"userbean":{"Uid":"100196","Showname":"\u75af\u72c2\u7684\u7334\u5b50","Avtar":null,"State":1}} 分析代码如下: // TODO 状态处理 500

【Android】解析JSON数据详解

Android解析JSON数据详解 JSON(JavaScript Object Notation) 定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式,获得了当今大部分语言的支持),从而可以在不同平台间进行数据交换.JSON采用兼容性很高的文本格式,同时也具备类似于C语言体系的行为. – Json.org JSON的结构: (1) Name/Value Pairs(无序的):类似所熟知的Keyed list. Has

Android 之json解析2

JSON(JavaScript Object Notation) 定义:字符串 键值对 解析方法有JSON,谷歌GSON,阿里巴巴FastJSON 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性. 业内主流技术为其提供了完整的解决方案(有点类似于正则表达式,获得了当今大部分语言的支持),从而可以在不同平台间进行数据交换. JSON采用兼容性很高的文本格式,同时也具备类似于C语言体系的行为. Json建构于两种结构: 1.“名称/值”对的集合(A collection of name