将 JSON 从/assets 文件夹读取到 Android 中的 ArrayList 中? [英] Read JSON from /assets folder into an ArrayList in Android?

查看:34
本文介绍了将 JSON 从/assets 文件夹读取到 Android 中的 ArrayList 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我对这个问题研究了很多.我学会了如何从我的资产文件夹中的文本文件中读取.问题是我不想以字符串结尾,因为该文件实际上是使用 json 编写的数组列表,并且在该数组列表中是对象.我只需要访问一个对象.这就是我的意思:

First, I have researched this question a lot. I learned how to read from my text file in the assets folder. The problem is that I don't want to end up with a string because the file is actually an arraylist written using json and inside that arraylist are objects. I need to access one object only. Here's what I mean:

我有一个 Book 类,它有一个称为章节的整数、一个称为标题的整数和一个称为 pageNum 的整数.我创建了几个 Book 对象并将它们添加到一个 ArrayList.然后我使用以下代码将我的 ArrayList 写入文本文件(在常规 java 项目中):

I have a Book class, which has an int called chapter, an int called title, and an int called pageNum. I created several Book objects and added them to an ArrayList. Then I used the following code to write my ArrayList to a text file (in a regular java project):

ArrayList<Book> aList = new ArrayList<Book>();
aList.add(book1);
//etc...add more Book objects here...

File aFile = new File("books.txt");
FileOutputStream aFileStream = new FileOutputStream(aFile);
JSONOutputStream jsonOut = new JSONOutputStream(aFileStream);
jsonOut.writeObject(aList);
jsonOut.close();

该代码创建了一个文本文件,然后我将其放入我的 Android 项目的/assets 文件夹中,因为我希望它包含在应用程序中.在非 Android java 项目中,我可以简单地使用以下代码重新填充 ArrayList,以便我可以从特定索引解析 Book 对象:

That code creates a text file which I then put into the /assets folder in my Android project because I want it included with the app. In a non-Android java project I could simply use the following code to repopulate an ArrayList so that I could parse Book obects from specific indexes:

File bFile = new File("books.txt");
FileInputStream bFileStream = new FileInputStream(bFile);
JSONInputStream jsonIn = new JSONInputStream(bFileStream);
Arraylist<Book> bList = (ArrayList<Book>) jsonIn.readObject();
Book aBook = bList.get(253); //some arbitrary index

我使用的 json 代码来自 quickconnectfamily.org.您必须将名为 qc_json.jar 的文件添加到项目的构建路径中.http://www.quickconnectfamily.org/qcjson/more.html

The json code I'm using comes from quickconnectfamily.org. You have to add a file called qc_json.jar to the build path of your project. http://www.quickconnectfamily.org/qcjson/more.html

Android 中的问题是当我使用 InputStream 读取文件时,我只能将整个文件变成一个字符串,上面的代码在 Android 中不起作用.我不能将 JSONInputStreams 包裹在 InputStream 周围,只能包裹在 FileInputStream 周围.但似乎我无法使用文件输入流.

The problem in Android is when I read the file using InputStream, I can only get the entire file into a string, the code above doesn't work in Android. I can't wrap JSONInputStreams around an InputStream, only around a FileInputStream. But it seems I am unable to use FileInputStream.

所以我需要的是一种在我的 Android 应用程序中创建 ArrayList 而不是字符串的方法.在不透露太多关于我的应用程序的情况下,该应用程序基本上会生成一个随机数并根据 ArrayList 中的该索引创建一个 Book 对象.然后向用户询问来自该特定书籍的信息.听起来很傻,但真正的应用程序要酷得多.

So what I need is a way to create an ArrayList rather than a string in my Android app. Without giving away too much about my app, the app basically generates a random number and creates a Book object from that index in the ArrayList. Then the user is quizzed with info from that specific book. Sounds silly, but the real app is much cooler.

我对解决方案、在文本文件中存储对象的替代方法等持开放态度.请不要简单地批评我的语法、语法或应用程序想法.我对应用程序开发还很陌生,我不太在意个人意见.如果有人想看我可以上传更多代码,但目前似乎没有必要.谢谢.

I'm open to solutions, alternative methods of storing objects in a text file, etc. Please don't simply post criticism about my grammar, syntax, or application idea. I'm pretty new to app development and I couldn't care less about personal opinions. If anyone wants to see more code I can upload it, but it doesn't seem necessary at this point. Thanks.

推荐答案

我想出了解决方案.我连接了我的 Evo 4G 并测试了该应用程序并且它工作正常.这是我所做的:

I figured out the solution. I connected my Evo 4G and tested the app and it worked. Here's what I did:

我用下面的方法从文件中读取,这也是我之前在做的:

I used the following method to read from the file, which is what I was doing before:

InputStream is = appContext.getAssets().open("books.txt");
int size = is.available();
buffer = new byte[size];
is.read(buffer);
is.close();
String bufferString = new String(buffer);

当你这样做时,你最终会得到一个包含整个文件的字符串.这是我以前能够做的,但我想要的是一种将 String 转换为 Book 对象的 ArrayList 的方法.这是我如何实现的:

When you do this, you end up with a String of the entire file. This is what I was able to do before, but what I wanted was a way to convert the String to an ArrayList of Book objects. Here's how I accomplished this:

//convert string to JSONArray
jsonArray = new JSONArray(bufferString);
//parse an Object from a random index in the JSONArray
JSONObject anObject = jsonArray.getJSONObject(randomNum); 

Book aBook = new Book();
aBook.setTitle((String) anObject.get("title"));
//you can continue to set the different attributes of the Book object using key/value pairs from the Book class (e.g. setPageNum, setChapter, etc).

我不知道,也许这对某些人来说很明显,但我真的找不到任何这样做的例子.在另一个问题中,有人提到使用 Android 原生的 json 库 org.json 所以我试过了,它奏效了.

I don't know, maybe that was obvious to some people, but I really couldn't find any examples that did this. In a different question someone mentioned using the json library native to Android org.json and so I tried that and it worked.

这篇关于将 JSON 从/assets 文件夹读取到 Android 中的 ArrayList 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆