阅读从JSON / assets文件夹到Android中一个ArrayList? [英] Read JSON from /assets folder into an ArrayList in Android?

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

问题描述

首先,我研究这个问题很多。我学会了如何从资产的文件夹我的文本文件读取。问题是,我不希望最终有一个字符串,因为该文件实际上是使用JSON写一个ArrayList和数组列表里面都是对象。我需要访问只有一个对象。这就是我的意思是:

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类,其中有一章叫一个int,一个int叫称号,并呼吁页次一个int。我创建了几个书对象并将其添加到一个ArrayList。然后我用下面的code写我的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();

这code创建我然后放入/资产文件夹在我的Andr​​oid项目,因为我希望它包含在应用程序中的文本文件。在非Android的Java项目,我可以简单地使用下面的code重新填充一个ArrayList,这样我可以从分析具体指标书obects:

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的$​​ C $ C我使用的是来自quickconnectfamily.org。你必须添加一个名为qc_json.jar到项目的构建路径文件。 http://www.quickconnectfamily.org/qcjson/more.html

在Android的问题是,当我读到使用InputStream中的文件,我只能得到整个文件转换成字符串时,code以上的Andr​​oid不工作。我不能完成JSONInputStreams各地的InputStream,只有约一个FileInputStream。但似乎我无法使用 的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.

所以,我需要的是一个方法来创建一个ArrayList,而不是一个字符串,在我的Andr​​oid应用程序。 不放弃太多关于我的应用程序,该应用程序基本上产生一个随机数,并创建从该指数中的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.

我接受的解决方案,存储在一个文本文件中的对象等,请不要简单地张贴批评我的语法,句法,或应用的想法的替代方法。我是pretty的新应用程序的开发,我可以不关心个人的意见。如果有人想看看 更多code我可以上传它,但它似乎没有必要在这一点上。谢谢你。

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

当你这样做,你最终整个文件的字符串。这是我能够做到过,但我想要的是一种方法,将字符串转换为图书对象的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).

我不知道,也许这是很明显的一些人,但我真的无法找到这样做的任何实例。在一个不同的问题使用JSON库原生到Android 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天全站免登陆