Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android studio中解析此json文件?_Android_Json - Fatal编程技术网

如何在android studio中解析此json文件?

如何在android studio中解析此json文件?,android,json,Android,Json,由于文本没有任何特定的索引名称,如何解析此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", "\

由于文本没有任何特定的索引名称,如何解析此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\"",
    ]
    }
像这样:

JSONArray array = object.getJSONArray("titles");
    for(int i = 0; i < array.length(); i ++){
        String title = array.getString(i);

        //Do something with the string
    }
JSONArray数组=object.getJSONArray(“标题”);
对于(int i=0;i
JsonArray
视为字符串数组。
但是,在您提供的数组中,由于数组最后一个元素上的逗号,JSON无效,您需要删除此逗号才能获得有效的JSON。

它只是一个简单的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));
}
JSONArray-JSONArray=jsonObject.getJSONArray(“标题”);

对于(int i=0;i,您可以将其解析为此类的实例:

public class Instance{
   public String[] titles;
}
但是您需要删除home后面的最后一个逗号。如果逗号后面没有其他项目,则表示它不是有效的JSON字符串。

这应该可以

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

    string content = jsonArray.getString(i);

    ....

}
JSONObject-JSONObject=newjsonobject(yourString);
JSONArray JSONArray=jsonObject.getJSONArray(“标题”);
for(int i=0;i
试试这个:

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();
}
试试看{
JSONArray itemArray=jsonObject.getJSONArray(“标题”)
对于(int i=0;i
您还可以使用Gson库解析到对象中。