android Json Gson FastJson 解析
2016-09-05 20:24
603 查看
一 Json
xml
java
Main.java
Person.java
View Code
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
相关文章推荐
- Android常用Json解析框架GSON和FastJson的使用
- Android Jackson、Gson、FastJson解析框架对比
- Android总结之json解析(FastJson Gson 对比)
- Android-解析Json(Gson-FastJson)
- Android学习(二):解析JSON,GSON,FASTJSON;JDBC编程
- Android进阶笔记14:3种JSON解析工具(org.json、fastjson、gson)
- Android JSON 解析库的使用 - Gson 和 fast-json
- Android json解析(FastJson Gson 对比)
- Android之解析JSON数据示例(android原生态,FastJson,Gson)
- Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例
- Android中的JSON解析方式:json; Gson ; Fastjson
- Android中Json数据解析(二)--使用Gson、Jackson和FastJson解析Json数据
- Android使用GSON和FASTJSON解析服务器返回的JSON数据
- android JSON解析 fastjson和gson的使用
- android json解析(fastjson,Gson等)
- Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例
- Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例
- Android之json解析(FastJson Gson 对比)
- Android中Json数据解析(二)--使用Gson、Jackson和FastJson解析Json数据
- Android框架之路——Json解析一篇就够(原生技术+Gson+FastJson)