标签:
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 }
标签:
原文地址:http://www.cnblogs.com/zhongyinghe/p/5430350.html