{
"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