简体   繁体   English

从/ assets文件夹中读取JSON到Android中的ArrayList?

[英]Read JSON from /assets folder into an ArrayList in Android?

First, I have researched this question a lot. 首先,我已经研究了很多这个问题。 I learned how to read from my text file in the assets folder. 我学会了如何从assets文件夹中的文本文件中读取。 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. 问题是我不想最终得到一个字符串,因为该文件实际上是一个使用json编写的arraylist,其中arraylist是对象。 I need to access one object only. 我只需要访问一个对象。 Here's what I mean: 这就是我的意思:

I have a Book class, which has an int called chapter, an int called title, and an int called pageNum. 我有一个Book类,它有一个名为chapter的int,一个名为title的int,以及一个名为pageNum的int。 I created several Book objects and added them to an ArrayList. 我创建了几个Book对象并将它们添加到ArrayList中。 Then I used the following code to write my ArrayList to a text file (in a regular java project): 然后我使用以下代码将我的ArrayList写入文本文件(在常规Java项目中):

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();

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

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

The json code I'm using comes from quickconnectfamily.org. 我正在使用的json代码来自quickconnectfamily.org。 You have to add a file called qc_json.jar to the build path of your project. 您必须将名为qc_json.jar的文件添加到项目的构建路径中。 http://www.quickconnectfamily.org/qcjson/more.html http://www.quickconnectfamily.org/qcjson/more.html

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. Android中的问题是当我使用InputStream读取文件时,我只能将整个文件转换为字符串,上面的代码在Android中不起作用。 I can't wrap JSONInputStreams around an InputStream, only around a FileInputStream. 我不能围绕InputStream包装JSONInputStreams,只能围绕FileInputStream。 But it seems I am unable to use FileInputStream. 但似乎我无法使用FileInputStream。

So what I need is a way to create an ArrayList rather than a string in my Android app. 所以我需要的是一种在我的Android应用程序中创建ArrayList而不是字符串的方法。 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. 在没有过多关于我的应用程序的情况下,应用程序基本上生成一个随机数,并从ArrayList中的该索引创建一个Book对象。 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. 谢谢。

I figured out the solution. 我找到了解决方案。 I connected my Evo 4G and tested the app and it worked. 我连接了我的Evo 4G并测试了应用程序,它运行良好。 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);

When you do this, you end up with a String of the entire file. 执行此操作时,最终会得到整个文件的String。 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. 这是我以前能够做的,但我想要的是一种将String转换为Book对象的ArrayList的方法。 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).

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. 在一个不同的问题,有人提到使用原生于 Android org.json的json库,所以我尝试了它,它工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM