在android解析json
2016-04-25 11:41
447 查看
1、采用一般方式解释json为对象
2、采用一般方式解析json为List
3、采用一般方式解析json为map
4、使用gson进行解析
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 中使用KSOAP2调用WebService
- Android Studio 使用记录——快捷键、常用操作
- Android之下拉与上拉刷新
- Android 内存优化总结
- android studio生成aar包并在其他工程引用aar包
- Android ViewGroup中有关测量的方法
- Android--Fragment
- 在android中生成json数据
- android studio学习---标签页分离,满足查同一个文件的不同部分
- android---------版本控制工具Git的使用
- 关于Android侧使用AES进行加解密时的key和iv使用的方法
- Android Studio 2.0+高效开发之路
- Android ActionBar完全解析,使用官方推荐的最佳导航栏(上)
- Java建造者模式,Android建造者模式的AlertDialog
- Android判断网路是否畅通加权限
- 解决Android studio中 logcat等乱码问题
- Android ViewDragHelper完全解析 自定义ViewGroup神器
- Android 深入理解Android中的自定义属性
- Android 手把手教您自定义ViewGroup(一)
- Android 自定义View (四) 视频音量调控