android Json Gson FastJson 解析
一 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.domain;
public class Person {
private int id;
private String name;
public Person(int id, String name, String address) {
super();
this.id = id;
this.name = name;
this.address = address;
}
private String address;
public int getId() {
return id;
}
@Override
public String toString() {
return "Person [address=" + address + ", id=" + id + ", name=" + name
+ "]";
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Person() {
// TODO Auto-generated constructor stub
}
}
HttpUtils.java
package com.android.myjson.http; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; public class HttpUtils { public HttpUtils() {
// TODO Auto-generated constructor stub
} public static String getJsonContent(String url_path) {
try {
URL url = new URL(url_path);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setConnectTimeout(3000);
connection.setRequestMethod("GET");
connection.setDoInput(true);
int code = connection.getResponseCode();
if (code == 200) {
return changeInputStream(connection.getInputStream());
}
} catch (Exception e) {
// TODO: handle exception
}
return "";
} private static String changeInputStream(InputStream inputStream) {
// TODO Auto-generated method stub
String jsonString = "";
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int len = 0;
byte[] data = new byte[1024];
try {
while ((len = inputStream.read(data)) != -1) {
outputStream.write(data, 0, len);
}
jsonString = new String(outputStream.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonString;
}
}
JsonTools.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;
}
}
android Json Gson FastJson 解析的更多相关文章
- Json,Gson,FastJson解析笔记
Json,Gson,FastJson解析笔记 1.将JavaBean转换成Json对象: public static String CreatJsonFromObject(Object key,Obj ...
- Android JSON,Gson,fastjson实现比较
activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- android使用Gson来解析json
Gson是一种对象的解析json,非常好用,介绍一个站点http://json.parser.online.fr/能够帮我们看一个字符串是不是Json 对于Json文件 { "id" ...
- JSON 之FastJson解析
http://blog.sina.com.cn/s/blog_7ffb8dd501013qas.html 一.阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具 ...
- (转)JSON 之FastJson解析
一.阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parse ...
- Android Json生成及解析实例
JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据 ...
- Android进阶笔记17:3种JSON解析工具(org.json、fastjson、gson)
一. 目前解析json有三种工具:org.json(Java常用的解析),fastjson(阿里巴巴工程师开发的),Gson(Google官网出的),其中解析速度最快的是Gson. 3种json工具下 ...
- Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例
Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例 继上篇json解析,我用了原生的json解析,但是在有些情况下我们不得不承认,一些优秀的json解析框架确实十分的 ...
- Android进阶笔记14:3种JSON解析工具(org.json、fastjson、gson)
一. 目前解析json有三种工具:org.json(Java常用的解析),fastjson(阿里巴巴工程师开发的),Gson(Google官网出的),其中解析速度最快的是Gson. 3种json工具下 ...
随机推荐
- 你知道吗?使用任何HTML5开发工具都可开发iOS、Android原生App
APICloud App开发平台一直在不断升级开发工具库,这一年增加了众多开发工具.目的就是让开发者可以选择使用任何自己喜欢的HTML5开发工具去开发App.这次,APICloud把所有关于开发工具的 ...
- JqGrid自定义的列
$("#gridTable").jqGrid({ //...其它属性 colModel: [ //...其它列 { name: 'dsource_alarm', index: 'd ...
- 利用pip安装模块(以安装pyperclip为例)
>任务:利用pip安装pyperclip模块 >前提:你已经在你的电脑里面安装啦Python2.7的Windows版本,并且已经配置了环境变量 >实现步骤 >>打开你的P ...
- spark 源码安装
clone 源码 git clone git://github.com/apache/spark.git maven编译源码 国外镜像比较慢,此处修改maven仓库的镜像为阿里云镜像: <mir ...
- cocos2d-x 运行时xcode提示错误:"vtable for XXX", referenced from 问题已解决;
vtable/引用和虚函数相关,今天在添加一个层的时候报了这个错误,很低级的错误,忘了实现虚函数了(谨记!!) 若如果实现了虚函数还依然如此的话,可能是创建的时候忘了钩上 -desktop 选项了,把 ...
- 返回值是JSON的阿贾克斯方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Android 自定义view(二) —— attr 使用
前言: attr 在前一篇文章<Android 自定义view -- attr理解>已经简单的进行了介绍和创建,那么这篇文章就来一步步说说attr的简单使用吧 自定义view简单实现步骤 ...
- Android 蓝牙API详解
随着近两年可穿戴式产品逐渐进入人们的生活,蓝牙开发也成为了Android开发的一个重要模块,下面我们就来说一说蓝牙的这些API. 1.蓝牙开发有两个主要的API: BuletoothAdapter:本 ...
- ubuntu配置tftp服务
ubuntu配置TFTP服务: TFTP是用来下载远程文件的最简单的网络协议,基于UDP协议.xinetd是新一代的网络守护进程服务程序,经常用于管理多种轻量型internet服务. sudo apt ...
- css2 [lang|=en] 误区
[lang|=en] w3c说明:css2选择器,选择以en开头的的lang属性. w3c的这个解释是有误区的,en开头,但是en后面必须要有-,也就是说是选择的是en-开头