|
9 | 9 | import cn.leancloud.sms.LCCaptchaDigest; |
10 | 10 | import com.google.gson.Gson; |
11 | 11 | import com.google.gson.GsonBuilder; |
| 12 | +import com.google.gson.JsonElement; |
12 | 13 | import com.google.gson.reflect.TypeToken; |
13 | 14 | import junit.framework.TestCase; |
14 | 15 |
|
@@ -149,6 +150,26 @@ public void testDoubleAndLong() { |
149 | 150 | System.out.println(responses.toString()); |
150 | 151 | } |
151 | 152 |
|
| 153 | + public void testObjectDeserialization() { |
| 154 | + String input = "{\"result\":{\"objectId\":\"637233023f103a0cf936caf8\",\"billId\":\"202211142022269827\"," + |
| 155 | + "\"state\":-1,\"size\":5,\"price\":10.6,\"payPrice\":0,\"isPaid\":false," + |
| 156 | + "\"createdAt\":\"2022-11-14T12:22:26.122Z\",\"isCommented\":false,\"purpose\":\"用于小说配音\"," + |
| 157 | + "\"user\":{\"objectId\":\"5e5462617796d9006a09c795\",\"name\":\"方冶(全天接单主页加微)\"," + |
| 158 | + "\"iconUrl\":\"http://file2.i7play.com/j6ptbHjPSAaBurvOYMPRgevtTzbwXYNF/CROP_20221112164622689.jpg\"," + |
| 159 | + "\"id\":2230377,\"wx\":\"ZmZxy-18\",\"qq\":\"\",\"isVip\":true,\"is18\":true},\"isBu\":false}} "; |
| 160 | + Type ResponseMap = new TypeToken<Map<String, Object>>() {}.getType(); |
| 161 | + Map<String, Object> result = GsonWrapper.getGsonInstance().fromJson(input, ResponseMap); |
| 162 | + Object data = result.get("result"); |
| 163 | + JsonElement jsonElement = GsonWrapper.getGsonInstance().toJsonTree(data); |
| 164 | + System.out.println(jsonElement); |
| 165 | + if (data instanceof Map) { |
| 166 | + Map<String,Object> dataMap = (Map<String,Object>)data; |
| 167 | + Object priceObj = dataMap.get("price"); |
| 168 | + System.out.println(priceObj); |
| 169 | + } |
| 170 | + System.out.println(result.toString()); |
| 171 | + } |
| 172 | + |
152 | 173 | public void testNumberParser() { |
153 | 174 | Number numbers[] = {3, 4.5, 5.0, -0, 0.0, 0.0002, -0.0002, -5, -6.5}; |
154 | 175 | for (Number num: numbers) { |
|
0 commit comments