您的位置:首页 > 移动开发 > Android开发

在android解析json

2016-04-25 11:41 447 查看
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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: