EasyJson
很容易处理JSONObject&JSONArray的工具。
使用了大概200行的代码,对原JSONObject进行简单封装,使用反射和泛型实现String->ESONObect的转换。
类结构
ESON 提供JSONObject与JSONArray相关的静态方法处理类
ESONObject extends JSONObject;
ESONArray extends JSONArray;
详见源码:https://github.com/isong0623/EasyJson
如何使用ESONObject?
//build.gradle
dependencies {
...
api "com.github.isong0623:EasyJson:1.0-support"
...
}
示例代码
//generate a json object 生成json对象
String strJson = "{\"a\":[\"1\",\"2\"],\"b\":{\"a\":\"0\"},\"int\":2,\"string\":\"str\",\"long\":12345665498765431,\"double\":45.7565123456}";
JSONObject obj = getJSONObject(strJson);//ESON.getJSONObject(strJson);
ESONObject ejb = new ESONObject(strJson);//or ESONObject.getESONObject(strJson);
//compare obj with ejb 两个对象比较
System.out.println(String.format("obj is equals ejb :%b",obj.toString().equals(ejb.toString())));//obj is equals ejb :true
System.out.println(ejb.toString()+"\n");//{"a":["1","2"],"b":{"a":"0"},"string":"str","double":45.7565123456,"int":2,"long":12345665498765431}
//get the data in ESONObject 从对象中取出数据
int i = ejb.getJSONValue("int",0);//or ESON.getJSONValue(obj,"int",0);
System.out.println(i);//2
long l = ejb.getJSONValue("long",0L);//or ESON.getJSONValue(obj,"long",0L);
System.out.println(l);//12345665498765431
double d = ejb.getJSONValue("double",0D);//or ESON.getJSONValue(obj,"double",0D);
System.out.println(d);//45.7565123456
float f = ejb.getJSONValue("double",0f);//or ESON.getJSONValue(obj,"double",0f);
System.out.println(f+"\n");//45.75651
//insert data 插入数据
ejb
.putValue("int2",123456789)
.putValue("long2",3028903829038290382L)
.putValue("string2","123456789");
//search again 取出插入的数据
System.out.println(ejb.getJSONValue("int2",1));//123456789
System.out.println(ejb.getJSONValue("long2",new Long(0L)));//3028903829038290382
System.out.println(ejb.getJSONValue("string2",new String("-"))+"\n");//123456789
//exceptions
System.out.println(ejb.getJSONValue("int2","1")+"//int -> string");//123456789//int -> string
System.out.println(ejb.getJSONValue("long2",1)+"//long -> int");//1//long -> int
System.out.println(ejb.getJSONValue("string2",1)+"//string -> int");//123456789//string -> int
示例代码输出
obj is equals ejb :true
{"a":["1","2"],"b":{"a":"0"},"string":"str","double":45.7565123456,"int":2,"long":12345665498765431}
2
12345665498765431
45.7565123456
45.75651
123456789
3028903829038290382
123456789
123456789//int -> string
1//long -> int
123456789//string -> int
如何使用ESONArray?
示例代码
String strArray = "[\"11111111111111111111111111111\",2.2,3333333333333,4444,5.555555555555]";
JSONArray jArr = getJSONArray(strArray);
ESONArray eArr = new ESONArray(strArray);//or ESON.getJSONArray(strArray);
System.out.println("jArr equals eArr ? "+eArr.toString().equals(jArr.toString()));
System.out.println(eArr.toString()+"\n");
Object objs[] = new Object[]{"1",2.0f,3L,4,5D};//normally value
for(int i=0,ni= eArr.length();i<ni;++i){
System.out.println(eArr.getArrayValue(i,objs[i]));
}
System.out.println();//toString
for(int i=0,ni= eArr.length();i<ni;++i){
System.out.println(eArr.getArrayValue(i,""));
}
eArr.putValue("123");
eArr.putValue(0,"456");
System.out.println();//toInteger
for(int i=0,ni= eArr.length();i<ni;++i){
System.out.println(eArr.getArrayValue(i,1));
}
示例代码输出
jArr equals eArr ? true
["11111111111111111111111111111",2.2,3333333333333,4444,5.555555555555]
11111111111111111111111111111
2.2
3333333333333
4444
5.555555555555
11111111111111111111111111111
2.2
3333333333333
4444
5.555555555555
456
1
1
4444
1
123
其他
ESON.setDebugable(true);//print the ESON Exception default false 打印ESON异常日志 默认false
来源:CSDN
作者:best335
链接:https://blog.csdn.net/best335/article/details/104727051