推荐使用的库(直接看gradle文件吧):
apply plugin: 'com.android.library'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.android.support:recyclerview-v7:23.2.1'
compile 'io.reactivex:rxandroid:1.0.1'
compile 'io.reactivex:rxjava:1.0.17'
compile 'com.jakewharton:butterknife:8.4.0'
provided 'org.greenrobot:eventbus:3.0.0'
compile 'com.alibaba:fastjson:1.2.18'
compile 'com.google.dagger:dagger:2.2'
apt 'com.google.dagger:dagger-compiler:2.2'
}
一些基本使用:
FastJson:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.util.List;
import java.util.Map;
/**
* Created by yaphetzhao on 2016/10/19.
* JsonUtil
* alibaba fastjson
*/
public class JsonUtil {
// 序列化:
// String jsonString = JSON.toJSONString(obj);
// 反序列化:
// VO vo = JSON.parseObject("...", VO.class);
// 泛型反序列化:
// import com.alibaba.fastjson.TypeReference;
// List<VO> list = JSON.parseObject("...", new TypeReference<List<VO>>() {});
/**
* 把JSON数据转换成普通字符串列表
*
* @param jsonData JSON数据
* @throws Exception
*/
public static List<String> getStringList(String jsonData) throws Exception {
return JSON.parseArray(jsonData, String.class);
}
/**
* 把JSON数据转换成指定的java对象
*
* @param jsonData JSON数据
* @param clazz 指定的java对象
* @throws Exception
*/
public static <T> T getSingleBean(String jsonData, Class<T> clazz)
throws Exception {
return JSON.parseObject(jsonData, clazz);
}
/**
* 把JSON数据转换成指定的java对象列表
*
* @param jsonData JSON数据
* @param clazz 指定的java对象
* @throws Exception
*/
public static <T> List<T> getBeanList(String jsonData, Class<T> clazz)
throws Exception {
return JSON.parseArray(jsonData, clazz);
}
/**
* 把JSON数据转换成较为复杂的java对象列表
*
* @param jsonData JSON数据
* @throws Exception
*/
public static List<Map<String, Object>> getBeanMapList(String jsonData)
throws Exception {
return JSON.parseObject(jsonData,
new TypeReference<List<Map<String, Object>>>() {
});
}
/**
* 将网络请求下来的数据用fastjson处理空的情况,并将时间戳转化为标准时间格式
*
* @param result
*/
public static String dealResponseResult(String result) {
result = JSONObject.toJSONString(result,
SerializerFeature.WriteClassName,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullBooleanAsFalse,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullNumberAsZero,
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteDateUseDateFormat,
SerializerFeature.WriteEnumUsingToString,
SerializerFeature.WriteSlashAsSpecial,
SerializerFeature.WriteTabAsSpecial);
return result;
}
}
其他:
懒得写