FastJson
public class FastJson {
public static final int JSON_ARRAY=1;
public static final int JSON_OBJECT=2;
public static final int JSON_ERRO=3;
/**
* 暴露API 给调用层调用
* @param json
* @param clazz
* @return
*/
public static Object pareseObject(String json,Class clazz)
{
Object object=null;
Class> jsonClass=null;
//JSONArray 类型
if(json.charAt(0)=='[')
{
try {
object=toList(json,clazz);
} catch (JSONException e) {
e.printStackTrace();
}
}else if(json.charAt(0)=='{')
{
try {
JSONObject jsonObject=new JSONObject(json);
//反射得到最外层的model 作为返回值返回 一定要有空的构造方法 User
object=clazz.newInstance();
/*
得到的最外层的key集合
*/
Iterator> iterator=jsonObject.keys();
//遍历集合
while (iterator.hasNext())
{
String key= (String) iterator.next();
Object fieldValue=null;
//得到当前clazz类型的所有成员变量
List fields=getAllFields(clazz,null);
for (Field field:fields)
{
//将key和成员变量进行匹配
if(field.getName().equalsIgnoreCase(key))
{
field.setAccessible(true);
//得到 key所对应的值 值 可以基本类型 类类型
fieldValue=getFieldValue(field,jsonObject,key);
if(fieldValue!=null)
{
field.set(object,fieldValue);
}
field.setAccessible(false);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return object;
}
/**
* 得到当前的value值
* @param field
* @param jsonObject
* @param key
* @return
*/
private static Object getFieldValue(Field field, JSONObject jsonObject, String key) throws JSONException {
Object fieldValue=null;
//得到当前成员变量类型
Class> fieldClass=field.getType();
if(fieldClass.getSimpleName().toString().equals("int")
||fieldClass.getSimpleName().toString().equals("Integer"))
{
fieldValue=jsonObject.getInt(key);
}else if(fieldClass.getSimpleName().toString().equals("String"))
{
fieldValue=jsonObject.getString(key);
}else if(fieldClass.getSimpleName().toString().equals("double"))
{
fieldValue=jsonObject.getDouble(key);
}else if(fieldClass.getSimpleName().toString().equals("boolean")
)
{
fieldValue=jsonObject.getBoolean(key);
}else if(fieldClass.getSimpleName().toString().equals("long")
)
{
fieldValue=jsonObject.getLong(key);
}else
{
//判断集合类型 和对象类型 jsonValue 代表完整的json字符串 里面一层
String jsonValue=jsonObject.getString(key);
switch (getJSONType(jsonValue))
{
case JSON_ARRAY:
//List
Type fieldType=field.getGenericType();
if(fieldType instanceof ParameterizedType)
{
ParameterizedType parameterizedType= (ParameterizedType) fieldType;
//List 当前类 所实现的泛型 User
Type[] fieldArgType=parameterizedType.getActualTypeArguments();
for(Type type:fieldArgType)
{
//fieldArgClass 代表着User.class
Class> fieldArgClass= (Class>) type;
fieldValue=toList(jsonValue,fieldArgClass);
}
}
break;
case JSON_OBJECT:
//剥下来的json字符串 fieldClass 成员变量类型
fieldValue=pareseObject(jsonValue,fieldClass);
break;
case JSON_ERRO:
break;
}
}
return fieldValue;
}
/**
* 获取当前json字符串的类型
*
* @param jsonValue
* @return
*/
private static int getJSONType(String jsonValue) {
char firstChar=jsonValue.charAt(0);
if(firstChar=='{')
{
return JSON_OBJECT;
}else if(firstChar=='[')
{
return JSON_ARRAY;
}else
{
return JSON_ERRO;
}
}
/**
* 解析JsonArray数组
* @param json
* @param clazz
* @return
*/
private static Object toList(String json, Class clazz) throws JSONException {
List
public class News {
private int id;
private String title;
private String content;
private User author;
private List reader;//List
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public User getAuthor() {
return author;
}
public void setAuthor(User author) {
this.author = author;
}
public List getReader() {
return reader;
}
public void setReader(List reader) {
this.reader = reader;
}
@Override
public String toString() {
return "News [author=" + author + ", content=" + content + ", id=" + id
+ ", reader=" + reader + ", title=" + title + "]";
}
}
public class MainActivity extends AppCompatActivity {
private static final String TAG = "test";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 能 1
* 不能 2
* @param view
*/
public void click(View view)
{
News news = new News();
news.setId(1);
news.setTitle("新年放假通知");
news.setContent("从今天开始放假啦。");
news.setAuthor(createAuthor());
news.setReader(createReaders());
String json=FastJson.toJson(news);
Log.d(TAG, FastJson.toJson(news));
User user= (User) FastJson.pareseObject(json,User.class);
}
public void convert(View view)
{
News news = new News();
news.setId(1);
news.setTitle("新年放假通知");
news.setContent("从今天开始放假啦。");
news.setAuthor(createAuthor());
news.setReader(createReaders());
String json=FastJson.toJson(news);
Log.d(TAG, FastJson.toJson(news));
News news1= (News) FastJson.pareseObject(json,News.class);
Log.i(TAG," moedel "+news1.toString());
}
private static List createReaders() {
List readers = new ArrayList();
User readerA = new User();
readerA.setId(2);
readerA.setName("Jack");
readers.add(readerA);
User readerB = new User();
readerB.setId(1);
readerB.setName("Lucy");
readerB.setPwd("123456789");
readers.add(readerB);
return readers;
}
private static User createAuthor() {
User author = new User();
author.setId(1);
author.setName("Fancyy");
author.setPwd("123456");
return author;
}
}