json解析
gson解析封装类
package com.android.mygson.gson;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
;
public class GsonTools {
public GsonTools() {
// TODO Auto-generated constructor stub
}
/**
* @param <T>
* @param jsonString
* @param cls
* @return
*/
public static <T> T getPerson(String jsonString, Class<T> cls) {
T t = null;
try {
Gson gson = new Gson();
t = gson.fromJson(jsonString, cls);
} catch (Exception e) {
// TODO: handle exception
}
return t;
}
/**
* 使用Gson进行解析 List<Person>
*
* @param <T>
* @param jsonString
* @param cls
* @return
*/
public static <T> List<T> getPersons(String jsonString, Class<T> cls) {
List<T> list = new ArrayList<T>();
try {
Gson gson = new Gson();
list = gson.fromJson(jsonString, new TypeToken<List<T>>() {
}.getType());
} catch (Exception e) {
}
return list;
}
/**
* @param jsonString
* @return
*/
public static List<String> getList(String jsonString) {
List<String> list = new ArrayList<String>();
try {
Gson gson = new Gson();
list = gson.fromJson(jsonString, new TypeToken<List<String>>() {
}.getType());
} catch (Exception e) {
// TODO: handle exception
}
return list;
}
public static List<Map<String, Object>> listKeyMaps(String jsonString) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try {
Gson gson = new Gson();
list = gson.fromJson(jsonString,
new TypeToken<List<Map<String, Object>>>() {
}.getType());
} catch (Exception e) {
// TODO: handle exception
}
return list;
}
}
界面调用
package com.android.mygson;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.android.mygson.domain.Person;
import com.android.mygson.gson.GsonTools;
import com.android.mygson.http.HttpUtils;
public class Main extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private Button person, persons, liststring, listmap;
private static final String TAG = "Main";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
person = (Button) this.findViewById(R.id.person);
persons = (Button) this.findViewById(R.id.persons);
liststring = (Button) this.findViewById(R.id.liststring);
listmap = (Button) this.findViewById(R.id.listmap);
person.setOnClickListener(this);
persons.setOnClickListener(this);
liststring.setOnClickListener(this);
listmap.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.person:
String path = "http://192.168.0.102:8080/jsonProject/servlet/JsonAction?action_flag=person";
String jsonString = HttpUtils.getJsonContent(path);
Person person = GsonTools.getPerson(jsonString, Person.class);
Log.i(TAG, person.toString());
break;
case R.id.persons:
String path2 = "http://192.168.0.102:8080/jsonProject/servlet/JsonAction?action_flag=persons";
String jsonString2 = HttpUtils.getJsonContent(path2);
List<Person> list = GsonTools.getPersons(jsonString2, Person.class);
Log.i(TAG, list.toString());
break;
case R.id.liststring:
String path3 = "http://192.168.0.102:8080/jsonProject/servlet/JsonAction?action_flag=liststring";
String jsonString3 = HttpUtils.getJsonContent(path3);
List<String> list2 = GsonTools.getList(jsonString3);
Log.i(TAG, list2.toString());
break;
case R.id.listmap:
String path4 = "http://192.168.0.102:8080/jsonProject/servlet/JsonAction?action_flag=listmap";
String jsonString4 = HttpUtils.getJsonContent(path4);
List<Map<String,Object>> list3 = GsonTools.listKeyMaps(jsonString4);
Log.i(TAG, list3.toString());
break;
}
}
}
android技术交流群:207833859
posted on 2015-06-16 09:27 XiNanHeiShao 阅读(194) 评论(0) 编辑 收藏 举报