Skip to content

Commit 9052543

Browse files
committed
Client:优化自动生成代码中value为JSONArray对应的生成代码,解决toArray(count, page, ")拼接错误
1 parent 30499a9 commit 9052543

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import apijson.demo.R;
3333
import apijson.demo.StringUtil;
3434

35+
import com.alibaba.fastjson.JSONArray;
3536
import com.alibaba.fastjson.JSONObject;
3637

3738
/**自动生成代码
@@ -65,7 +66,6 @@ protected void onCreate(Bundle savedInstanceState) {
6566
tvAutoResponse = (TextView) findViewById(R.id.tvAutoResponse);
6667

6768

68-
6969
String request = "{\"Moment\":{\"id\":551},\"[]\":{\"count\":3,\"page\":1,\"Comment\":{\"momentId@\":\"Moment/id\",\"@column\":\"id,userId,content\"}}}";
7070

7171
tvAutoRequest.setText(StringUtil.getString(JSON.format(request)));
@@ -80,7 +80,7 @@ public void copy(View v) {
8080
public void auto(View v) {
8181
auto(StringUtil.getString(tvAutoRequest));
8282
}
83-
83+
8484
public void get(View v) {
8585
request((TextView) v);
8686
}
@@ -127,7 +127,7 @@ public void request(String method) {
127127
, JSON.parseObject(StringUtil.getString(tvAutoRequest)), false), REQUEST_TO_REQUEST);
128128
}
129129

130-
130+
131131

132132

133133

@@ -157,10 +157,14 @@ public static String parse(final String name, final JSONObject request) {
157157
String pairKey;
158158
for (String key : set) {
159159
value = request.get(key);
160+
if (value == null) {
161+
continue;
162+
}
163+
160164
pairKey = new String(key instanceof String ? "\"" + key + "\"" : key);
161165
if (value instanceof JSONObject) {//APIJSON Array转为常规JSONArray
162166
if (isArrayKey(key)) {//APIJSON Array转为常规JSONArray
163-
response += NEWLINE + NEWLINE + "//" + key + "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";
167+
response += NEWLINE + NEWLINE + "//" + key + "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";
164168

165169
int count = ((JSONObject) value).getIntValue(JSONRequest.KEY_COUNT);
166170
int page = ((JSONObject) value).getIntValue(JSONRequest.KEY_PAGE);
@@ -173,9 +177,9 @@ public static String parse(final String name, final JSONObject request) {
173177
String prefix = key.substring(0, key.length() - 2);
174178
response += NEWLINE + NEWLINE
175179
+ parentKey + ".add(" + getItemKey(key) + ".toArray("
176-
+ count + ", " + page + (prefix.isEmpty() ? "" : ", " + prefix) + "));";
180+
+ count + ", " + page + (prefix.isEmpty() ? "" : ", \"" + prefix + "\"") + "));";
177181

178-
response += NEWLINE + "//" + key + ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + NEWLINE;
182+
response += NEWLINE + "//" + key + ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + NEWLINE;
179183
} else {//常规JSONObject,往下一级提取
180184
response += NEWLINE + NEWLINE + "//" + key + "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";
181185

@@ -185,7 +189,20 @@ public static String parse(final String name, final JSONObject request) {
185189
response += NEWLINE + "//" + key + ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + NEWLINE;
186190
}
187191
} else {//其它Object,直接填充
188-
value = value instanceof String ? new String("\"" + value + "\"") : value;
192+
if (value instanceof String) {
193+
value = "\"" + value + "\"";
194+
} else if (value instanceof JSONArray) {
195+
String s = StringUtil.getString(value);
196+
if (s.startsWith("[")) {
197+
s = s.substring(1);
198+
}
199+
if (s.endsWith("]")) {
200+
s = s.substring(0, s.length() - 1);
201+
}
202+
// String type = ((JSONArray) value).getClass().getSimpleName();
203+
// value = "new " + type.substring(0, type.length() - 2) + "[]{" + s + "}";
204+
value = "new Object[]{" + s + "}";//反射获取泛型太麻烦,反正开发中还要改的
205+
}
189206

190207
response += NEWLINE + parentKey + ".put(" + pairKey + ", " + value + ");";
191208
}

0 commit comments

Comments
 (0)