大家好,我有這樣的 json 回應
Response response = service.execute(requestSendToNS);
// JSONObject jsonResponse = new JSONObject(response);
//String data = jsonResponse.getString("id");
System.out.println("Response Body : " response.getBody());
結果如下:
Response Body : [{"status":"success","message":"update success","id":"1404","internalid":2604},{"status":"failed","message":"bad location is already used in another location","id":1405}]
我的問題是如何從我的 json 回應中獲取值“id”?
我嘗試使用此代碼:
// JSONObject jsonResponse = new JSONObject(response);
//String data = jsonResponse.getString("id");
我也有使用這個串列 responseObject = objectMapper.readValue(response.getBody(),List);
但我無法從 json 陣列映射到物件
但我無法從 id 中檢索價值
uj5u.com熱心網友回復:
您的回應正文是一個陣列。
[{...},{...}]
這就是為什么你不能得到id
String data = jsonResponse.getString("id");
請查看 JsonReader 以將 json 讀取為 JsonArray
https://docs.oracle.com/javaee/7/api/javax/json/JsonReader.html#readObject--
JsonReader jsonReader = Json.createReader(new StringReader(response.getBody()));
JsonArray array = jsonReader.readArray();
JsonObject obj = array.getJsonObject(0);
String data = obj.getString("id");
uj5u.com熱心網友回復:
首先,您必須創建一個與JSON回應具有完全相同結構的類,然后您可以使用ObjectMapperfromjackson庫將JSONto 類寫入并從中讀取值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/389623.html
