{
"quote": "To understand the heart and mind of a person, look not at
what he has already achieved, but at what he aspires to.",
"author": "Kahlil Gibran (1883-1931)"
}
如何在沒有任何對象名稱如何解析此JSON沒有對象名稱
{
"quote": "To understand the heart and mind of a person, look not at
what he has already achieved, but at what he aspires to.",
"author": "Kahlil Gibran (1883-1931)"
}
如何在沒有任何對象名稱如何解析此JSON沒有對象名稱
怎麼辦呢解析呢?
如果你在你的代碼滿足{}
,您可以使用JSONObject
解析它。
如果您在代碼中遇到[]
,則可以使用JSONArray
來解析它。
如果您在代碼中遇到[]
,則可以使用for loop
來獲取其中的值。您的代碼中應該使用try catch
。
試試這個。
try {
JSONObject jsonObject = new JSONObject(response);
String author = jsonObject.getString("author");
String quote = jsonObject.getString("quote");
} catch (JSONException e) {
e.printStackTrace();
}
這是絕對正常的json。
JSONObject json = new JSONObject(your_json);
String quote = json.getString("quote");
String author = json.getString("author");
有不同的方法來解析JSON對象
簡單的方式
JSONObject myJson = new JSONObject(yourJsonString);
String quote = myJson. getString("quote");
String author = myJson. getString("author");
另一個和recomented方法是使用GSON 解析JSON爲此,您可以參閱下面的鏈接
How to parse json parsing Using GSON in android
https://www.journaldev.com/2321/gson-example-tutorial-parse-json