Android 如何解析这个没有对象名的Json

Android 如何解析这个没有对象名的Json,android,json,Android,Json,如何在没有任何对象名称的情况下解析它如何进行 如果在代码中遇到{},可以使用JSONObject对其进行解析 如果在代码中遇到[],可以使用JSONArray对其进行解析 如果在代码中遇到[],则可以使用for loop从中获取值 您应该在代码中使用try catch 试试这个 { "quote": "To understand the heart and mind of a person, look not at what he has already achieved,

如何在没有任何对象名称的情况下解析它

如何进行

  • 如果在代码中遇到
    {}
    ,可以使用
    JSONObject
    对其进行解析

  • 如果在代码中遇到
    []
    ,可以使用
    JSONArray
    对其进行解析

  • 如果在代码中遇到
    []
    ,则可以使用
    for loop
    从中获取值

  • 您应该在代码中使用
    try catch

试试这个

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

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");
另一种推荐的方法是使用Gson解析json 对此,您可以参考以下链接

JSONObject myJson = new JSONObject(yourJsonString);
String quote = myJson. getString("quote");
String author = myJson. getString("author");