Android 用 JSONArray 成员打包对象
我正在开发一个 Android 应用程序,我面临的情况是,我需要通过将我创建的类的对象传递到捆绑包中来启动一个新活动,以便它可以在新活动中使用。
我研究了如何做到这一点,对于对象来说,最好的方法似乎是使它们可打包。但是,问题是该对象的类的字段之一是 JSONArray 类型,并且似乎不支持 JSONArray?或者也许我只是看起来不够努力,或者我错过了一些东西,但我已经搜索了几个小时,但我仍然找不到任何东西。
来打包对象的字段
更具体地说,我需要用writeX();
。其中 X 可以是整数、字符串等。但是,正如我刚才所说,出于特定原因,我的字段之一是 JSONArray 类型(无论如何我都不想更改它),而且 Parcel 似乎没有支持这个吗?
谢谢
I'm developing an Android app and I'm in a situation where I need to start a new activity by passing in an object of a Class that I created into a bundle so it can be used in the new activity.
I've researched on how to do this and it seems like for objects the best way is to make them Parcelable. However, the problem is that one of the fields for that object's class is a JSONArray type and it doesn't seem like JSONArray is supported? Or maybe I just haven't looked hard enough or I missed something, but I've been searching for hours and i still couldn't find anything.
To be more specific, I need to parcel the fields of my object with a
writeX();
where the X can such things as ints, strings etc. However, like I just said, one of my fields is a JSONArray type for a specific reason (I do not wish to change this in anyway) and it seems like Parcel doesn't support this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢您的回复,但不幸的是,这个 JSONArray 包含太多变化的信息,无法正常工作(它有整数字符串、JSONObjects 甚至 JSONArrays -_-)。不过我找到了一种方法,那就是使用 toString 方法将整个 JSONArray 转换为字符串。这使得可以将其放入捆绑包中。我真的不喜欢这样做......但我没有太多时间思考这个问题太久:P。无论如何,谢谢
thanks for the reply, but unfortunately this JSONArray contains way too much varying info for this to work (it has integers strings, JSONObjects and even JSONArrays -_-). I have however found a way, and this is to convert the entire JSONArray to a string by using the toString method. This makes it possible to put it into a Bundle. I don't really like doing it this way... but I don't have much time to be thinking on this for too long :P. Thanks anyway
我能够使用 putSerialized 而不是 putParcelable 将 JSONObjects 的 ArrayList 放入包中。
I was able to put an ArrayList of JSONObjects into a bundle using putSerializable rather than putParcelable.
你提前知道 JSONArray 是什么类型吗?如果是这样,您可以将 JSONArray 转换为已知类型的列表。
这里不支持 JSONArray,因为它不是实现 Parcelable 的类。如果您完全无法控制 JSONArray 类型的选择,则需要将 JSONArray 转换为列表。
以下代码将已知 String 类型的 JSONArray 转换为 ArrayList:
Do you know ahead of time what the type the JSONArray is of? If so you can convert the JSONArray to a List of the known type.
JSONArray is not supported here because its not a class that implements Parcelable. If you absolutely have no control over the choice of that JSONArray type you need to convert the JSONArray to a list.
The following code converts a JSONArray of a known String type to an ArrayList: