我想從我從API請求中收到的JSONObject中獲取一個字符串。從JSONObject中獲取字符串
我的JSONObject如下:
{
"status":"success",
"message":"user authenticated",
"data": { "id":11,
"username":"2160481",
"token":"MEznhD8RE1-ykZLOdGs4i9ZfRFQl5h_"
}
}
我試過沒有成功一些迴應,我發現在這裏,喜歡的getString(「令牌」)。我也試着用JSONObject創建一個數組,並從那裏獲取字符串,也沒有成功。
編輯:
嘗試1:
String data = response.toString();
JSONObject teste = new JSONObject(data);
String status = teste.getString("status");
System.out.println(status);
嘗試2:
String token = null;
try {
JSONArray arr = new JSONArray();
arr.put(response);
JSONObject jObj = arr.getJSONObject(0);
token = jObj.getString("token");
System.out.println("---> Token: " + token);
} catch (JSONException e) {
System.out.println("---> Error:" + e);
}
嘗試3:
String token = null;
try {
token = response.getString("token");
System.out.println("---> Token: " + token);
} catch (JSONException e) {
System.out.println("---> Error:" + e);
}`