Skip to content

Commit 13c3ee4

Browse files
committed
Client:Test工程中AutoActivity新增[]解析java
1 parent aa9641a commit 13c3ee4

2 files changed

Lines changed: 114 additions & 46 deletions

File tree

APIJSON(Android)/APIJSON(ADT)/APIJSONTest/res/layout/auto_activity.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@
4141
android:padding="0dp"
4242
android:text="uri" />
4343

44+
<Button
45+
android:id="@+id/btnAutoCreate"
46+
android:layout_width="wrap_content"
47+
android:layout_height="match_parent"
48+
android:onClick="create"
49+
android:text="Create" />
50+
4451
<Button
4552
android:id="@+id/btnAutoAuto"
4653
android:layout_width="wrap_content"

APIJSON(Android)/APIJSON(ADT)/APIJSONTest/src/apijson/demo/ui/AutoActivity.java

Lines changed: 107 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,20 @@
1414

1515
package apijson.demo.ui;
1616

17-
import static zuo.biao.apijson.StringUtil.bigAlphaPattern;
18-
1917
import java.util.Set;
2018

2119
import zuo.biao.apijson.JSON;
2220
import zuo.biao.apijson.JSONRequest;
2321
import zuo.biao.apijson.JSONResponse;
24-
import zuo.biao.apijson.Log;
2522
import android.app.Activity;
2623
import android.content.Context;
2724
import android.content.Intent;
2825
import android.os.Bundle;
26+
import android.util.Log;
2927
import android.view.View;
3028
import android.widget.EditText;
3129
import apijson.demo.R;
3230
import apijson.demo.StringUtil;
33-
import apijson.demo.model.User;
3431

3532
import com.alibaba.fastjson.JSONObject;
3633

@@ -62,61 +59,75 @@ protected void onCreate(Bundle savedInstanceState) {
6259

6360

6461

65-
String request = "{\"User\":{\"id\":1,\"@column\":\"id,name\"},\"Moment\":{\"userId@\":\"/User/id\"}}";
62+
String request = "{\"User\":{\"id\":1,\"@column\":\"id,name\"},\"[]\":{\"count\":3,\"page\":1,\"Moment\":{\"userId@\":\"User/id\"}}}";
6663

6764
tvAutoResult.setText(StringUtil.getString(JSON.format(request)));
6865

69-
70-
7166
}
7267

7368

69+
public void create(View v) {
70+
JSONRequest request = new JSONRequest();
7471

75-
public void auto(View v) {
76-
auto(StringUtil.getString(tvAutoResult));
77-
}
78-
public void auto(String request) {
79-
String response = parse("", JSON.parseObject(request)); //newObjectRequest(request);
72+
//User<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
73+
JSONRequest userRequest = new JSONRequest();
74+
userRequest.put("id", 1);
75+
userRequest.put("@column", "id,name");
8076

81-
Log.d(TAG, "\n request = \n" + request);
82-
Log.d(TAG, "\n response = \n" + response);
77+
request.put("User", userRequest);
78+
//User>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
8379

84-
tvAutoResult.setText(StringUtil.getString(response));
85-
}
8680

81+
//[]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
82+
JSONRequest item = new JSONRequest();
8783

88-
public static final String NEWLINE = "\n";
84+
//Moment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
85+
JSONRequest momentRequest = new JSONRequest();
86+
momentRequest.put("userId@", "User/id");
8987

90-
private String newObjectRequest(String json) {
91-
JSONRequest request = new JSONRequest();
88+
item.put("Moment", momentRequest);
89+
//Moment>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
9290

9391

94-
//单个JSONObject<<<<<<<<<<<<<<<<<
95-
JSONRequest userRequest = new JSONRequest();
96-
97-
User user = new User();
98-
user.setId((long) 1);
92+
request.add(item.toArray(0, 0));
93+
//[]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
9994

100-
userRequest.add(JSON.parseObject(user));
95+
96+
String json = StringUtil.getString(JSON.format(request));
97+
Log.d(TAG, "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n request = \n" + json + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
98+
99+
tvAutoResult.setText(json);
100+
}
101101

102-
userRequest.setColumn("id,name");
103-
//单个JSONObject>>>>>>>>>>>>>>>>>>
104102

105103

106-
request.put("User", userRequest);
104+
public void auto(View v) {
105+
auto(StringUtil.getString(tvAutoResult));
106+
}
107+
public void auto(String request) {
108+
String response = parse("", JSON.parseObject(request)); //newObjectRequest(request);
107109

110+
Log.d(TAG, "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n request = \n" + request + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
111+
Log.d(TAG, "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n response = \n" + response + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
108112

109-
return JSON.format(request);
113+
tvAutoResult.setText(StringUtil.getString(response));
110114
}
111115

112116

117+
public static final String NEWLINE = "\n";
118+
119+
/**
120+
* @param name
121+
* @param request
122+
* @return
123+
*/
113124
public static String parse(final String name, final JSONObject request) {
114-
Log.i(TAG, "parse request = \n" + JSON.toJSONString(request));
125+
// Log.i(TAG, "parse request = \n" + JSON.toJSONString(request));
115126
if (request == null || request.isEmpty()) {
116-
Log.i(TAG, "parse request == null || request.isEmpty() >> return request;");
127+
// Log.i(TAG, "parse request == null || request.isEmpty() >> return request;");
117128
return null;
118129
}
119-
String parentKey = getTableKey(name);
130+
String parentKey = isArrayKey(name) ? getItemKey(name) : getTableKey(name);
120131

121132

122133
String response = NEWLINE + "JSONRequest " + parentKey + " = new JSONRequest();";
@@ -126,28 +137,44 @@ public static String parse(final String name, final JSONObject request) {
126137

127138
Object value;
128139
String pairKey;
129-
String arrayKey;
130140
for (String key : set) {
131141
value = request.get(key);
132142
pairKey = new String(key instanceof String ? "\"" + key + "\"" : key);
133143
if (value instanceof JSONObject) {//APIJSON Array转为常规JSONArray
134144
if (isArrayKey(key)) {//APIJSON Array转为常规JSONArray
135-
// arrayKey = key.substring(0, key.indexOf(JSONResponse.KEY_ARRAY));
136-
} else {//常规JSONObject,往下一级提取
145+
response += NEWLINE + NEWLINE + "//" + key + "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";
137146

138-
response += NEWLINE + parse(key, (JSONObject) value);
147+
int count = ((JSONObject) value).getIntValue(JSONRequest.KEY_COUNT);
148+
int page = ((JSONObject) value).getIntValue(JSONRequest.KEY_PAGE);
149+
((JSONObject) value).remove(JSONRequest.KEY_COUNT);
150+
((JSONObject) value).remove(JSONRequest.KEY_PAGE);
151+
152+
response += parse(key, (JSONObject) value);
153+
154+
155+
String prefix = key.substring(0, key.length() - 2);
156+
response += NEWLINE + NEWLINE
157+
+ parentKey + ".add(" + getItemKey(key) + ".toArray("
158+
+ count + ", " + page + (prefix.isEmpty() ? "" : ", " + prefix) + "));";
159+
160+
response += NEWLINE + "//" + key + ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + NEWLINE;
161+
} else {//常规JSONObject,往下一级提取
162+
response += NEWLINE + NEWLINE + "//" + key + "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";
139163

140-
response += NEWLINE + NEWLINE + parentKey + ".put(" + pairKey + ", " + getTableKey(key) + ");" + NEWLINE;
164+
response += parse(key, (JSONObject) value);
165+
166+
response += NEWLINE + NEWLINE + parentKey + ".put(" + pairKey + ", " + getTableKey(key) + ");";
167+
response += NEWLINE + "//" + key + ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + NEWLINE;
141168
}
142169
} else {//其它Object,直接填充
143170
value = value instanceof String ? new String("\"" + value + "\"") : value;
144-
171+
145172
response += NEWLINE + parentKey + ".put(" + pairKey + ", " + value + ");";
146173
}
147174
}
148175
}
149176

150-
Log.i(TAG, "parse return response = \n" + response);
177+
// Log.i(TAG, "parse return response = \n" + response);
151178
return response;
152179
}
153180

@@ -156,23 +183,57 @@ public static String parse(final String name, final JSONObject request) {
156183

157184

158185
/**获取Table变量名
159-
* @param key => StringUtil.getNoBlankString(key)
160-
* @return empty ? "list" : key + "List" 且首字母小写
186+
* @param key
187+
* @return empty ? "request" : key + "Request" 且首字母小写
161188
*/
162189
private static String getTableKey(String key) {
190+
return addSuffix(key, "Request");
191+
}
192+
/**获取Table变量名
193+
* @param key
194+
* @return empty ? "request" : key + "Request" 且首字母小写
195+
*/
196+
private static String getItemKey(String key) {
197+
return addSuffix(key.substring(0, key.length() - 2), "Item");
198+
}
199+
/**
200+
* @param key
201+
* @param suffix
202+
* @return key + suffix,第一个字母小写
203+
*/
204+
private static String addSuffix(String key, String suffix) {
163205
key = StringUtil.getNoBlankString(key);
164206
if (key.isEmpty()) {
165-
return "request";
207+
return firstCase(suffix);
166208
}
167209

168-
String first = key.substring(0, 1);
169-
if (bigAlphaPattern.matcher(first).matches()) {
170-
key = first.toLowerCase() + key.substring(1, key.length());
210+
return firstCase(key) + firstCase(suffix, true);
211+
}
212+
/**
213+
* @param key
214+
*/
215+
private static String firstCase(String key) {
216+
return firstCase(key, false);
217+
}
218+
/**
219+
* @param key
220+
* @param upper
221+
* @return
222+
*/
223+
private static String firstCase(String key, boolean upper) {
224+
key = StringUtil.getString(key);
225+
if (key.isEmpty()) {
226+
return "";
171227
}
172-
return key + "Request";
228+
229+
String first = key.substring(0, 1);
230+
key = (upper ? first.toUpperCase() : first.toLowerCase()) + key.substring(1, key.length());
231+
232+
return key;
173233
}
174234

175235

236+
176237
private static String getSimpleName(String arrayKey) {
177238
return JSONResponse.getSimpleName(arrayKey);
178239
}

0 commit comments

Comments
 (0)