什么是Moshi?
moshi是square團(tuán)隊(duì)發(fā)布在GitHub上的Json解析庫 GitHub
用法
- 首先是compile:
compile 'com.squareup.moshi:moshi:1.2.0'
- 根據(jù)Json字符串的格式建立實(shí)體
class PersonList {
Map<String, Person> list;
}
class Person {
int age;
Sex sex;
}
enum Sex {
MAN,
WOMAN
}
- 使用moshi進(jìn)行Json解析
String json = "xxxxxxx";
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<PersonList> jsonAdapter = moshi.adapter(PersonList.class);
try {
PersonList personList = jsonAdapter.fromJson(json);
} catch (Exception e) {
Log.e("json parse error", "parsePersonList: ", e);
}