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

android Json Gson FastJson 解析

2016-09-05 20:24 603 查看
一 Json

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/person" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="解析Person数据" />
<Button android:id="@+id/persons" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="解析List嵌套Person数据" />
<Button android:id="@+id/liststring" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="解析List嵌套String数据" />
<Button android:id="@+id/listmap" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="解析ListMap数据" />
</LinearLayout>


java

Main.java

package com.android.myjson;

import java.util.List;
import java.util.Map;

import com.android.myjson.domain.Person;
import com.android.myjson.http.HttpUtils;
import com.android.myjson.json.JsonTools;

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;

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.11.249:8080/jsonProject/servlet/JsonAction?action_flag=person";
String jsonString = HttpUtils.getJsonContent(path);
Person person = JsonTools.getPerson("person", jsonString);
Log.i(TAG, person.toString());
break;
case R.id.persons:
String path2 = "http://192.168.11.249:8080/jsonProject/servlet/JsonAction?action_flag=persons";
String jsonString2 = HttpUtils.getJsonContent(path2);
List<Person> list2 = JsonTools.getPersons("persons", jsonString2);
Log.i(TAG, list2.toString());
break;
case R.id.liststring:
String path3 = "http://192.168.11.249:8080/jsonProject/servlet/JsonAction?action_flag=liststring";
String jsonString3 = HttpUtils.getJsonContent(path3);
List<String> list3 = JsonTools.getList("liststring", jsonString3);
Log.i(TAG, list3.toString());
break;
case R.id.listmap:
String path4 = "http://192.168.11.249:8080/jsonProject/servlet/JsonAction?action_flag=listmap";
String jsonString4 = HttpUtils.getJsonContent(path4);
List<Map<String, Object>> list4 = JsonTools.listKeyMaps("listmap",
jsonString4);
Log.i(TAG, list4.toString());
break;
}
}
}


  Person.java

package com.android.myjson.json;

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 com.android.myjson.domain.Person;

/**
* 完成对json数据的解析
*
* @author jack
*
*/
public class JsonTools {

public JsonTools() {
// TODO Auto-generated constructor stub
}

public static Person getPerson(String key, String jsonString) {
Person person = new Person();
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject personObject = jsonObject.getJSONObject("person");
person.setId(personObject.getInt("id"));
person.setName(personObject.getString("name"));
person.setAddress(personObject.getString("address"));
} catch (Exception e) {
// TODO: handle exception
}
return person;
}

public static List<Person> getPersons(String key, String jsonString) {
List<Person> list = new ArrayList<Person>();
try {
JSONObject jsonObject = new JSONObject(jsonString);
// 返回json的数组
JSONArray jsonArray = jsonObject.getJSONArray(key);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
Person person = new Person();
person.setId(jsonObject2.getInt("id"));
person.setName(jsonObject2.getString("name"));
person.setAddress(jsonObject2.getString("address"));
list.add(person);
}
} catch (Exception e) {
// TODO: handle exception
}
return list;
}

public static List<String> getList(String key, String jsonString) {
List<String> list = new ArrayList<String>();
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray(key);
for (int i = 0; i < jsonArray.length(); i++) {
String msg = jsonArray.getString(i);
list.add(msg);
}
} catch (Exception e) {
// TODO: handle exception
}
return list;
}

public static List<Map<String, Object>> listKeyMaps(String key,
String jsonString) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray(key);
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 json_key = iterator.next();
Object json_value = jsonObject2.get(json_key);
if (json_value == null) {
json_value = "";
}
map.put(json_key, json_value);
}
list.add(map);
}
} catch (Exception e) {
// TODO: handle exception
}
return list;
}
}


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