【问题标题】:How can i parse this json file in android studio? [duplicate]我如何在 android studio 中解析这个 json 文件? [复制]
【发布时间】:2017-02-24 08:32:22
【问题描述】:

我如何解析这个 json 文件,因为文本没有任何特定的索引名称

 {
      "titles": [
        " Thoughts On The Works Of Providence",
        "\"All Is Vanity, Saith the Preacher\"",
        "\"And the sins of the fathers shall be\"",
        "\"Arcturus\" is his other name",
        "\"By the Waters of Babylon.\"",
        "\"De Gustibus--\"",
        "\"Faith\" is a fine invention",
        "\"Faithful to the end\" Amended",
        "\"Heaven\" -- is what I cannot reach!",
        "\"Heaven\" has different Signs -- to me --",
        "\"Heavenly Father\" -- take to thee",
        "\"Home\"",
    ]
    }

【问题讨论】:

    标签: android json


    【解决方案1】:

    像这样:

    JSONArray array = object.getJSONArray("titles");
        for(int i = 0; i < array.length(); i ++){
            String title = array.getString(i);
    
            //Do something with the string
        }
    

    您将JsonArray 视为字符串数组。 但是,在您提供的数组中,由于数组最后一个元素上的逗号,JSON 无效,您需要删除此逗号才能获得有效的 JSON。

    【讨论】:

    • 也可以使用Gson库解析成对象。
    【解决方案2】:

    这只是一个简单的 JSON。这样做。

    String json = "{\"titles\":[\" Thoughts On The Works Of Providence\",\"\\\"All Is Vanity, Saith the Preacher\\\"\",\"\\\"And the sins of the fathers shall be\\\"\",\"\\\"Arcturus\\\" is his other name\",\"\\\"By the Waters of Babylon.\\\"\",\"\\\"De Gustibus--\\\"\",\"\\\"Faith\\\" is a fine invention\",\"\\\"Faithful to the end\\\" Amended\",\"\\\"Heaven\\\" -- is what I cannot reach!\",\"\\\"Heaven\\\" has different Signs -- to me --\",\"\\\"Heavenly Father\\\" -- take to thee\",\"\\\"Home\\\"\"]}";
    
    JSONObject jsonObject = new JSONObject(json);
    

    以及访问您的项目:

    JSONArray jsonArray = jsonObject.getJSONArray("titles");
    for(int i=0;i<jsonArray.size(); i++){
        Log.d(TAG, jsonArray.getString(i));
    }
    

    【讨论】:

      【解决方案3】:

      您可以将其解析为此类的实例:

      public class Instance{
         public String[] titles;
      }
      

      但您需要删除 home 后的最后一个逗号。如果逗号后面没有其他项目,则表示它不是有效的 JSON 字符串。

      【讨论】:

        【解决方案4】:

        这应该可行。

        JSONObject jsonObject = new JSONObject(yourString);
        JSONArray jsonArray= jsonObject.getJSONArray("titles");
        for (int i=0; i < jsonArray.length(); i++) {
        
            string content = jsonArray.getString(i);
        
            ....
        
        }
        

        【讨论】:

          【解决方案5】:

          试试这个:

          Try{
             JSONArray itemArray = jsonObject.getJSONArray("titles")
             for (int i = 0; i < itemArray.length(); i++) {
                  String value=itemArray.getString(i);
                  Log.e("json", i+"="+value);
             }
          } catch (JSONException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          

          【讨论】:

            猜你喜欢
            • 2020-03-19
            • 2013-06-27
            • 1970-01-01
            • 2012-05-24
            • 2023-03-20
            • 1970-01-01
            • 1970-01-01
            • 2017-04-08
            相关资源
            最近更新 更多