Skip to content

Commit d719a07

Browse files
committed
优化JSONResponse解析
1 parent 3561696 commit d719a07

2 files changed

Lines changed: 48 additions & 29 deletions

File tree

APIJSON(Android)/APIJSON(ADT)/src/zuo/biao/apijson/client/JSONResponse.java

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,70 +51,84 @@ public Object get(String key) {
5151

5252

5353

54+
/**
55+
* @param object
56+
* @param clazz
57+
* @return
58+
*/
59+
public static <T> T getObject(com.alibaba.fastjson.JSONObject object, Class<T> clazz) {
60+
return JSON.parseObject(JSON.toJSONString(clazz == null ? object : object.getJSONObject(clazz.getSimpleName())), clazz);
61+
}
62+
// /**
63+
// * @param json
64+
// * @param clazz
65+
// * @return
66+
// */
67+
// public static <T> T getObject(String json, Class<T> clazz) {
68+
// return getObject(JSON.parseObject(json, clazz), clazz);
69+
// }
70+
71+
5472
/**
5573
* arrayObject = this
5674
* @param clazz
5775
* @return
5876
*/
59-
public <T> List<T> parseList(Class<T> clazz) {
60-
return parseList(this, clazz);
77+
public <T> List<T> getList(Class<T> clazz) {
78+
return getList(this, clazz);
6179
}
6280
/**
6381
* @param arrayObject
6482
* @param clazz
6583
* @return
6684
*/
67-
public static <T> List<T> parseList(com.alibaba.fastjson.JSONObject arrayObject, Class<T> clazz) {
68-
return JSON.parseArray(JSON.toJSONString(toJSONArray(arrayObject
85+
public static <T> List<T> getList(com.alibaba.fastjson.JSONObject arrayObject, Class<T> clazz) {
86+
return JSON.parseArray(JSON.toJSONString(getJSONArray(arrayObject
6987
, clazz == null ? null : clazz.getSimpleName())), clazz);
7088
}
7189

7290
/**
73-
* need try-catch
91+
* @param arrayKey
92+
* @param className
93+
* @return
7494
*/
7595
public JSONArray getJSONArray(String arrayKey, String className) {
76-
return isArrayKey(arrayKey) ? toJSONArray(getJSONObject(arrayKey), className) : super.getJSONArray(arrayKey);
96+
if (StringUtil.isNotEmpty(arrayKey, true) == false) {
97+
arrayKey = StringUtil.getString(className) + "[]";
98+
}
99+
return isArrayKey(arrayKey) ? getJSONArray(getJSONObject(arrayKey), className) : super.getJSONArray(arrayKey);
77100
}
78101

79102
/**
80103
* arrayObject = this
81104
* @return
82105
*/
83-
public JSONArray toJSONArray() {
84-
return toJSONArray(this, null);
106+
public JSONArray getJSONArray() {
107+
return getJSONArray(this, null);
85108
}
86109
/**
87110
* arrayObject = this
88-
* @param <T>
89111
* @return
90112
*/
91-
public <T> JSONArray toJSONArray(String className) {
92-
return toJSONArray(this, className);
93-
}
94-
/**
95-
* @param <T>
96-
* @param arrayJson
97-
* @return
98-
*/
99-
public static <T> JSONArray toJSONArray(String arrayJson, String className) {
100-
return toJSONArray(JSON.parseObject(arrayJson), className);
113+
public JSONArray getJSONArray(String className) {
114+
return getJSONArray(this, className);
101115
}
102116
/**
103117
* @param <T>
104118
* @param arrayObject
105119
* @param clazz
106120
* @return
107121
*/
108-
public static JSONArray toJSONArray(com.alibaba.fastjson.JSONObject arrayObject) {
109-
return toJSONArray(arrayObject, null);
122+
public static JSONArray getJSONArray(com.alibaba.fastjson.JSONObject arrayObject) {
123+
return getJSONArray(arrayObject, null);
110124
}
111125
/**
112126
* @param <T>
113127
* @param arrayObject
114128
* @param clazz
115129
* @return
116130
*/
117-
public static JSONArray toJSONArray(com.alibaba.fastjson.JSONObject arrayObject, String className) {
131+
public static JSONArray getJSONArray(com.alibaba.fastjson.JSONObject arrayObject, String className) {
118132
Set<String> set = arrayObject == null ? null : arrayObject.keySet();
119133
if (set == null || set.isEmpty()) {
120134
return null;
@@ -130,7 +144,7 @@ public static JSONArray toJSONArray(com.alibaba.fastjson.JSONObject arrayObject,
130144
}
131145
return JSON.parseArray(parentString);
132146
}
133-
147+
134148
//{"0":{...}, "1":{...}...}
135149

136150
className = StringUtil.getTrimedString(className);
@@ -152,4 +166,5 @@ public static JSONArray toJSONArray(com.alibaba.fastjson.JSONObject arrayObject,
152166
return array;
153167
}
154168

169+
155170
}

APIJSON(Android)/APIJSON(ADT)/src/zuo/biao/apijson/client/ui/QueryActivity.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import java.net.URLEncoder;
1919
import java.util.List;
2020

21-
import com.alibaba.fastjson.JSONArray;
22-
2321
import zuo.biao.apijson.JSON;
2422
import zuo.biao.apijson.client.HttpManager;
2523
import zuo.biao.apijson.client.HttpManager.OnHttpResponseListener;
@@ -29,6 +27,7 @@
2927
import zuo.biao.apijson.client.StringUtil;
3028
import zuo.biao.apijson.client.model.Comment;
3129
import zuo.biao.apijson.client.model.User;
30+
import zuo.biao.apijson.client.model.Work;
3231
import android.app.Activity;
3332
import android.content.Context;
3433
import android.content.Intent;
@@ -43,6 +42,8 @@
4342
import android.widget.TextView;
4443
import android.widget.Toast;
4544

45+
import com.alibaba.fastjson.JSONArray;
46+
4647
/**activity for request a query in Server
4748
* @author Lemon
4849
*/
@@ -172,17 +173,20 @@ public void onHttpResponse(int requestCode, final String resultJson, final Excep
172173
}
173174
JSONResponse response = new JSONResponse(resultJson);
174175
if (type == TYPE_ARRAY) {
175-
logList(JSONResponse.parseList(response.getJSONObject("User[]"), User.class));
176+
logList(JSONResponse.getList(response.getJSONObject("User[]"), User.class));
176177
} else if (type == TYPE_COMPLEX) {
177-
JSONArray array = JSONResponse.toJSONArray(response.getJSONObject("[]"));//, "Comment[]");//
178+
JSONArray array = JSONResponse.getJSONArray(response.getJSONObject("[]"));//, "Comment[]");//
178179
if (array == null || array.isEmpty()) {
179180
Log.e(TAG, "onHttpResponse type == TYPE_COMPLEX >> array == null || array.isEmpty() >> return;");
180181
return;
181182
}
182183
response = new JSONResponse(array.getJSONObject(0));
183184

184-
logList(JSONResponse.parseList(response, User.class));
185-
logList(JSONResponse.parseList(response == null ? null : response.getJSONObject("Comment[]"), Comment.class));
185+
User user = JSONResponse.getObject(response, User.class);
186+
Log.d(TAG, "onHttpResponse type == TYPE_COMPLEX >> user = " + JSON.toJSONString(user));
187+
Work work = JSONResponse.getObject(response, Work.class);
188+
Log.d(TAG, "onHttpResponse type == TYPE_COMPLEX >> work = " + JSON.toJSONString(work));
189+
logList(JSONResponse.getList(response == null ? null : response.getJSONObject("Comment[]"), Comment.class));
186190
}
187191

188192
runOnUiThread(new Runnable() {

0 commit comments

Comments
 (0)