【问题标题】:Android Load Json from file ErrorAndroid从文件错误加载Json
【发布时间】:2016-08-10 08:32:48
【问题描述】:

我正在构建一个可以离线工作的应用程序。

我将 json 下载到 sdcard 中的文件中。然后当我尝试使用 gson 加载它并将它与 POJO 类映射时,我得到错误:

读取文件:

File newList = new File(rootFolder.getParent(), "custom_list.new.dat");
String customListStr = Utils.readTextFile(newList);



    public static String readTextFile(File file) throws IOException {
        FileInputStream reader = new FileInputStream(file);
        StringBuffer data = new StringBuffer("");
        byte[] buffer = new byte[1024];
        while (reader.read(buffer) != -1)
            data.append(new String(buffer));
        reader.close();
        return data.toString();
    }



try to load it

//Load json file to Model
                    Gson gson = new GsonBuilder().create();
                    final MoviePojo response = gson.fromJson(customListStr, MoviePojo.class);
                    int size = response.getContentList().size();

:错误:com.google.gson.stream.MalformedJsonException:使用 JsonReader.setLenient(true) 在第 1 行第 3205 列路径 $ 接受格式错误的 JSON

【问题讨论】:

    标签: java android json gson retrofit


    【解决方案1】:

    该错误在文本中向您显示,您下载的 JSON 在其结构 MalformedJsonException 中存在错误,“格式错误”。检查 JSON 文件,从上到下,可能有一些 ("{", "[", ",", ":" ) 字符丢失或超过了它需要的数量。

    祝你好运。

    【讨论】:

    • 我刚刚检查过了。这是一个有效的 json
    • 进行此测试:复制 json 文件的内容并将其粘贴为变量“customListStr”的值。注释您创建 File 对象的行,如果再次显示错误,请运行您的代码。
    猜你喜欢
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 2018-04-12
    • 1970-01-01
    • 2015-07-03
    • 2020-07-22
    相关资源
    最近更新 更多