Skip to content

Commit 4fd4556

Browse files
committed
Server:优化排序,实现多层嵌套保持顺序;Client:优化JSON展示,结构清晰漂亮
1 parent e752d32 commit 4fd4556

File tree

5 files changed

+82
-23
lines changed

5 files changed

+82
-23
lines changed

APIJSON(Android)/APIJSON(ADT)/APIJSONDemoApp/src/apijson/demo/RequestUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static JSONObject newSingleRequest() {
6969

7070
public static JSONObject newColumnsRequest() {
7171
JSONObject object = new JSONObject(new User(38710));
72-
object.setColumns("id,name,phone");//StringUtil.getString(new String[]{"id", "name", "phone"}));//
72+
object.setColumns("id,name,phone");//测试排序通过 //StringUtil.getString(new String[]{"id", "name", "phone"}));//
7373
return new JSONRequest(User.class.getSimpleName(), object);
7474
}
7575

APIJSON(Android)/APIJSON(ADT)/APIJSONDemoApp/src/apijson/demo/ui/QueryActivity.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ private void query() {
153153

154154
final String fullUrl = getUrl(type);
155155

156-
tvQueryResult.setText("requesting...\n\n url = " + fullUrl + "\n\n request = \n" + request + "\n\n\n" + error);
156+
tvQueryResult.setText("requesting...\n\n url = " + fullUrl + "\n\n request = \n" + JSON.format(request) + "\n\n\n" + error);
157157
pbQuery.setVisibility(View.VISIBLE);
158158

159159
if (type < 10) {
@@ -282,8 +282,7 @@ public void run() {
282282
Toast.makeText(context, R.string.received_result, Toast.LENGTH_SHORT).show();
283283

284284
tvQueryResult.setText(e == null || JSON.isJsonCorrect(resultJson)
285-
? StringUtil.getTrimedString(resultJson) : e.getMessage() + "\n\n\n" + error);
286-
285+
? JSON.format(resultJson) : e.getMessage() + "\n\n\n" + error);
287286
}
288287
}
289288
});

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

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public class JSON {
3434
public static boolean isJsonCorrect(String s) {
3535
System.out.println(TAG + "isJsonCorrect <<<< " + s + " >>>>>>>");
3636
if (s == null
37-
// || s.equals("[]")
38-
// || s.equals("{}")
37+
// || s.equals("[]")
38+
// || s.equals("{}")
3939
|| s.equals("")
4040
|| s.equals("[null]")
4141
|| s.equals("{null}")
@@ -55,13 +55,21 @@ public static String getCorrectJson(String s) {
5555
}
5656

5757
/**json转JSONObject
58-
* @param s
58+
* @param json
5959
* @return
6060
*/
6161
public static JSONObject parseObject(String json) {
62+
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
63+
features |= Feature.OrderedField.getMask();
64+
return parseObject(json, features);
65+
}
66+
/**json转JSONObject
67+
* @param json
68+
* @param features
69+
* @return
70+
*/
71+
public static JSONObject parseObject(String json, int features) {
6272
try {
63-
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
64-
features |= Feature.SortFeidFastMatch.getMask();
6573
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), JSONObject.class, features);
6674
} catch (Exception e) {
6775
System.out.println(TAG + "parseObject catch \n" + e.getMessage());
@@ -70,14 +78,14 @@ public static JSONObject parseObject(String json) {
7078
}
7179

7280
/**json转实体类
73-
* @param s
81+
* @param json
7482
* @param clazz
7583
* @return
7684
*/
7785
public static <T> T parseObject(String json, Class<T> clazz) {
7886
try {
7987
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
80-
features |= Feature.SortFeidFastMatch.getMask();
88+
features |= Feature.OrderedField.getMask();
8189
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), clazz, features);
8290
} catch (Exception e) {
8391
System.out.println(TAG + "parseObject catch \n" + e.getMessage());
@@ -93,7 +101,7 @@ public static JSONArray parseArray(String json) {
93101
return com.alibaba.fastjson.JSON.parseArray(json);
94102
}
95103
/**json转实体类列表
96-
* @param s
104+
* @param json
97105
* @param clazz
98106
* @return
99107
*/
@@ -112,12 +120,34 @@ public static <T> List<T> parseArray(String json, Class<T> clazz) {
112120
*/
113121
public static String toJSONString(Object obj) {
114122
try {
115-
return com.alibaba.fastjson.JSON.toJSONString(obj, SerializerFeature.SortField);
123+
return com.alibaba.fastjson.JSON.toJSONString(obj);
124+
} catch (Exception e) {
125+
System.out.println(TAG + "toJSONString catch \n" + e.getMessage());
126+
}
127+
return null;
128+
}
129+
130+
/**实体类转json
131+
* @param obj
132+
* @param features
133+
* @return
134+
*/
135+
public static String toJSONString(Object obj, SerializerFeature... features) {
136+
try {
137+
return com.alibaba.fastjson.JSON.toJSONString(obj, features);
116138
} catch (Exception e) {
117139
System.out.println(TAG + "toJSONString catch \n" + e.getMessage());
118140
}
119141
return null;
120142
}
121143

144+
/**格式化,显示更好看
145+
* @param json
146+
* @return
147+
*/
148+
public static String format(String json) {
149+
return toJSONString(parseObject(json), SerializerFeature.PrettyFormat);
150+
}
151+
122152

123153
}

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/JSON.java

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414

1515
package zuo.biao.apijson;
1616

17+
import java.util.List;
18+
1719
import com.alibaba.fastjson.JSONArray;
1820
import com.alibaba.fastjson.JSONObject;
1921
import com.alibaba.fastjson.parser.Feature;
2022
import com.alibaba.fastjson.serializer.SerializerFeature;
2123

22-
import java.util.List;
23-
2424
/**阿里json封装类 防止解析时异常
2525
* @author Lemon
2626
*/
@@ -34,8 +34,8 @@ public class JSON {
3434
public static boolean isJsonCorrect(String s) {
3535
System.out.println(TAG + "isJsonCorrect <<<< " + s + " >>>>>>>");
3636
if (s == null
37-
// || s.equals("[]")
38-
// || s.equals("{}")
37+
// || s.equals("[]")
38+
// || s.equals("{}")
3939
|| s.equals("")
4040
|| s.equals("[null]")
4141
|| s.equals("{null}")
@@ -59,9 +59,17 @@ public static String getCorrectJson(String s) {
5959
* @return
6060
*/
6161
public static JSONObject parseObject(String json) {
62+
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
63+
features |= Feature.OrderedField.getMask();
64+
return parseObject(json, features);
65+
}
66+
/**json转JSONObject
67+
* @param json
68+
* @param features
69+
* @return
70+
*/
71+
public static JSONObject parseObject(String json, int features) {
6272
try {
63-
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
64-
features |= Feature.SortFeidFastMatch.getMask();
6573
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), JSONObject.class, features);
6674
} catch (Exception e) {
6775
System.out.println(TAG + "parseObject catch \n" + e.getMessage());
@@ -77,7 +85,7 @@ public static JSONObject parseObject(String json) {
7785
public static <T> T parseObject(String json, Class<T> clazz) {
7886
try {
7987
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
80-
features |= Feature.SortFeidFastMatch.getMask();
88+
features |= Feature.OrderedField.getMask();
8189
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), clazz, features);
8290
} catch (Exception e) {
8391
System.out.println(TAG + "parseObject catch \n" + e.getMessage());
@@ -112,12 +120,34 @@ public static <T> List<T> parseArray(String json, Class<T> clazz) {
112120
*/
113121
public static String toJSONString(Object obj) {
114122
try {
115-
return com.alibaba.fastjson.JSON.toJSONString(obj, SerializerFeature.SortField);
123+
return com.alibaba.fastjson.JSON.toJSONString(obj);
124+
} catch (Exception e) {
125+
System.out.println(TAG + "toJSONString catch \n" + e.getMessage());
126+
}
127+
return null;
128+
}
129+
130+
/**实体类转json
131+
* @param obj
132+
* @param features
133+
* @return
134+
*/
135+
public static String toJSONString(Object obj, SerializerFeature... features) {
136+
try {
137+
return com.alibaba.fastjson.JSON.toJSONString(obj, features);
116138
} catch (Exception e) {
117139
System.out.println(TAG + "toJSONString catch \n" + e.getMessage());
118140
}
119141
return null;
120142
}
121143

144+
/**格式化,显示更好看
145+
* @param json
146+
* @return
147+
*/
148+
public static String format(String json) {
149+
return toJSONString(parseObject(json), SerializerFeature.PrettyFormat);
150+
}
151+
122152

123153
}

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/server/sql/QueryHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ public JSONObject select(QueryConfig config) throws SQLException, Exception {
9595

9696
System.out.println(TAG + "select sql = " + sql);
9797

98-
JSONObject object = null;//new JSONObject();//null;
98+
JSONObject object = null;//new JSONObject(true);
9999
ResultSet rs = null;
100100
switch (config.getMethod()) {
101101
case POST:
102102
case PUT:
103103
case DELETE:
104104
int updateCount = statement.executeUpdate(sql);//创建数据对象
105105

106-
JSONObject result = newJSONObject(updateCount > 0 ? 200 : 1149, updateCount > 0 ? "success" : "failed,可能对象不存在!");
106+
JSONObject result = newJSONObject(updateCount > 0 ? 200 : 1149, updateCount > 0 ? "success" : "可能对象不存在!");
107107
result.put(Table.ID, config.getId());
108108
return result;
109109
default:

0 commit comments

Comments
 (0)