1414
1515package apijson .demo .ui ;
1616
17- import static zuo .biao .apijson .StringUtil .bigAlphaPattern ;
18-
1917import java .util .Set ;
2018
2119import zuo .biao .apijson .JSON ;
2220import zuo .biao .apijson .JSONRequest ;
2321import zuo .biao .apijson .JSONResponse ;
24- import zuo .biao .apijson .Log ;
2522import android .app .Activity ;
2623import android .content .Context ;
2724import android .content .Intent ;
2825import android .os .Bundle ;
26+ import android .util .Log ;
2927import android .view .View ;
3028import android .widget .EditText ;
3129import apijson .demo .R ;
3230import apijson .demo .StringUtil ;
33- import apijson .demo .model .User ;
3431
3532import 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