在Android应用程序资源使用JSON文件应用程序、文件、资源、Android

由网友(谁为谁红颜。、)分享简介:假设我有一个在我的应用程序的原始资源文件夹JSON内容的文件。我怎样才能阅读到的应用程序,这样我可以解析JSON?Suppose I have a file with JSON contents in the raw resources folder in my app. How can I read this in...

假设我有一个在我的应用程序的原始资源文件夹JSON内容的文件。我怎样才能阅读到的应用程序,这样我可以解析JSON?

Suppose I have a file with JSON contents in the raw resources folder in my app. How can I read this into the app, so that I can parse the JSON?

推荐答案

请参阅openRawResource.像这样的东西应该工作:

See openRawResource. Something like this should work:

InputStream is = getResources().openRawResource(R.raw.json_file);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
    Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    int n;
    while ((n = reader.read(buffer)) != -1) {
        writer.write(buffer, 0, n);
    }
} finally {
    is.close();
}

String jsonString = writer.toString();
阅读全文

相关推荐

最新文章