Skip to content

Commit 6c42d81

Browse files
committed
新增JSONResponse成功解析返回json;优化JSONObject,JSONRequest
1 parent 74085ad commit 6c42d81

7 files changed

Lines changed: 365 additions & 81 deletions

File tree

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import zuo.biao.apijson.client.StringUtil;
2020

21+
import com.alibaba.fastjson.JSONArray;
2122
import com.alibaba.fastjson.JSONObject;
2223
import com.alibaba.fastjson.parser.Feature;
2324
import com.alibaba.fastjson.serializer.SerializerFeature;
@@ -54,11 +55,11 @@ public static String getCorrectJson(String s) {
5455
* @param s
5556
* @return
5657
*/
57-
public static JSONObject parseObject(String s) {
58+
public static JSONObject parseObject(String json) {
5859
try {
5960
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
6061
features |= Feature.SortFeidFastMatch.getMask();
61-
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(s), JSONObject.class, features);
62+
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), JSONObject.class, features);
6263
} catch (Exception e) {
6364
System.out.println(TAG + "parseObject catch \n" + e.getMessage());
6465
}
@@ -70,25 +71,32 @@ public static JSONObject parseObject(String s) {
7071
* @param clazz
7172
* @return
7273
*/
73-
public static <T> T parseObject(String s, Class<T> clazz) {
74+
public static <T> T parseObject(String json, Class<T> clazz) {
7475
try {
7576
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
7677
features |= Feature.SortFeidFastMatch.getMask();
77-
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(s), clazz, features);
78+
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), clazz, features);
7879
} catch (Exception e) {
7980
System.out.println(TAG + "parseObject catch \n" + e.getMessage());
8081
}
8182
return null;
8283
}
83-
84+
85+
/**json转JSONArray
86+
* @param json
87+
* @return
88+
*/
89+
public static JSONArray parseArray(String json) {
90+
return com.alibaba.fastjson.JSON.parseArray(json);
91+
}
8492
/**json转实体类列表
8593
* @param s
8694
* @param clazz
8795
* @return
8896
*/
89-
public static <T> List<T> parseArray(String s, Class<T> clazz) {
97+
public static <T> List<T> parseArray(String json, Class<T> clazz) {
9098
try {
91-
return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(s), clazz);
99+
return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json), clazz);
92100
} catch (Exception e) {
93101
System.out.println(TAG + "parseArray catch \n" + e.getMessage());
94102
}
@@ -108,4 +116,5 @@ public static String toJSONString(Object obj) {
108116
return null;
109117
}
110118

119+
111120
}

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

Lines changed: 126 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,139 @@
1414

1515
package zuo.biao.apijson;
1616

17+
import static zuo.biao.apijson.client.HttpManager.UTF_8;
18+
19+
import java.io.UnsupportedEncodingException;
20+
import java.net.URLDecoder;
21+
import java.net.URLEncoder;
22+
import java.util.Set;
23+
24+
import zuo.biao.apijson.client.StringUtil;
25+
1726
/**use this class instead of com.alibaba.fastjson.JSONObject
1827
* @author Lemon
1928
*/
2029
public class JSONObject extends com.alibaba.fastjson.JSONObject {
2130
private static final long serialVersionUID = 8907029699680768212L;
2231

32+
/**ordered
33+
*/
2334
public JSONObject() {
2435
super(true);
2536
}
26-
27-
37+
public JSONObject(String json) {
38+
this(JSON.parseObject(json));
39+
}
40+
/**transfer com.alibaba.fastjson.JSONObject to JSONObject
41+
* @param object
42+
*/
43+
public JSONObject(com.alibaba.fastjson.JSONObject object) {
44+
this();
45+
add(object);
46+
}
47+
48+
49+
50+
/**
51+
* @param key
52+
* @return if value is String, return URLDecoder.decode((String) value, UTF_8);
53+
*/
54+
public Object getWithDecode(String key) {
55+
Object value = get(key);
56+
if (value instanceof String) {
57+
try {
58+
return URLDecoder.decode((String) value, UTF_8);
59+
} catch (UnsupportedEncodingException e) {
60+
e.printStackTrace();
61+
}
62+
}
63+
return value;
64+
}
65+
66+
67+
/**put(value.getClass().getSimpleName(), value);
68+
* @param value
69+
* @return
70+
*/
71+
public Object putWithEncode(Object value) {
72+
return putWithEncode(null, value);
73+
}
74+
/**
75+
* @param key
76+
* @param value if is String, value = URLEncoder.encode((String) value, UTF_8);
77+
* @return
78+
*/
79+
public Object putWithEncode(String key, Object value) {
80+
if (value instanceof String) {
81+
try {
82+
value = URLEncoder.encode((String) value, UTF_8);
83+
//just encode /, not need to encode [] ? URLEncoder.encode(key, UTF_8)
84+
} catch (UnsupportedEncodingException e) {
85+
e.printStackTrace();
86+
}
87+
}
88+
return super.put(StringUtil.isNotEmpty(key, true) ? key : value.getClass().getSimpleName(), value);
89+
}
90+
91+
/**put key-value in object into this
92+
* @param object
93+
*/
94+
public void add(com.alibaba.fastjson.JSONObject object) {
95+
Set<String> set = object == null ? null : object.keySet();
96+
if (set != null) {
97+
for (String key : set) {
98+
put(key, object.get(key));
99+
}
100+
}
101+
}
102+
103+
104+
//array object <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
105+
public static final String KEY_COUNT = "count";
106+
public static final String KEY_PAGE = "page";
107+
108+
public JSONObject setCount(int count) {
109+
put(KEY_COUNT, count);
110+
return this;
111+
}
112+
public int getCount() {
113+
return getIntValue(KEY_COUNT);
114+
}
115+
116+
public JSONObject setPage(int page) {
117+
put(KEY_PAGE, page);
118+
return this;
119+
}
120+
public int getPage() {
121+
return getIntValue(KEY_PAGE);
122+
}
123+
//array object >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
124+
125+
126+
127+
//judge <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
128+
/**
129+
* @param json
130+
* @return
131+
*/
132+
public static boolean isJSONObject(String json) {
133+
return JSON.parseObject(json) != null;
134+
}
135+
/**
136+
* @param key
137+
* @return
138+
*/
139+
public static boolean isObjectKey(String key) {
140+
key = StringUtil.getString(key);
141+
return StringUtil.isNotEmpty(key, false) && isArrayKey(key) == false && StringUtil.isAlpha(key.substring(0, 1));
142+
}
143+
/**
144+
* @param key
145+
* @return
146+
*/
147+
public static boolean isArrayKey(String key) {
148+
return key.endsWith("[]");
149+
}
150+
//judge >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
151+
28152
}

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

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414

1515
package zuo.biao.apijson.client;
1616

17-
import static zuo.biao.apijson.client.HttpManager.UTF_8;
18-
19-
import java.io.UnsupportedEncodingException;
20-
import java.net.URLEncoder;
21-
import java.util.Set;
22-
2317
import zuo.biao.apijson.JSONObject;
2418

2519
/**auto formatted request JSONObject
@@ -44,61 +38,19 @@ public JSONRequest(String name, Object object) {
4438
put(name, object);
4539
}
4640

47-
48-
@SuppressWarnings("unchecked")
49-
public <T> T get(String key) {
50-
return (T) super.get(key);
51-
}
52-
5341
/**put(value.getClass().getSimpleName(), value);
5442
* @param value
5543
* @return
5644
*/
5745
public Object put(Object value) {
58-
return put(null, value);
46+
return super.putWithEncode(value);
5947
}
6048
@Override
6149
public Object put(String key, Object value) {
62-
try {
63-
if (value instanceof String) {
64-
value = URLEncoder.encode((String) value, UTF_8);
65-
}
66-
return super.put(StringUtil.isNotEmpty(key, true) ? key : value.getClass().getSimpleName(), value);
67-
//just encode /, not need to encode [] ? URLEncoder.encode(key, UTF_8)
68-
} catch (UnsupportedEncodingException e) {
69-
e.printStackTrace();
70-
}
71-
return null;
50+
return super.putWithEncode(key, value);
7251
}
7352

74-
75-
/**put key-value in object into this
76-
* @param object
77-
*/
78-
public void add(JSONObject object) {
79-
Set<String> set = object == null ? null : object.keySet();
80-
if (set != null) {
81-
for (String key : set) {
82-
put(key, object.get(key));
83-
}
84-
}
85-
}
86-
87-
8853

89-
90-
public static final String KEY_COUNT = "count";
91-
public static final String KEY_PAGE = "page";
92-
93-
public JSONRequest setCount(Integer count) {
94-
put(KEY_COUNT, count);
95-
return this;
96-
}
97-
public JSONRequest setPage(Integer page) {
98-
put(KEY_PAGE, page);
99-
return this;
100-
}
101-
10254
/**create a parent JSONObject named []
10355
* @param count
10456
* @param page

0 commit comments

Comments
 (0)