安卓:转换Parcelable到JSON [英] Android: convert Parcelable to JSON

查看:1964
本文介绍了安卓:转换Parcelable到JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与发出消息到socket.io服务器socket.io图书馆工作。服务器期望JSON对象,数组等用我原来的执行JSONOject和JSONArray数据类型。不过,我想切换到使用通过 Parceler 库生成的类。与图书馆的注解生成的类可以被包裹成包裹。这似乎是管理这些通信的一个非常方便的方式。但是,有没有办法来Parceler的类或包裹类转换成JSON字符串或的JSONObject /阵列?

I am working with socket.io library which emits messages to the socket.io server. The server expects JSON objects, arrays, etc. My original implementation used JSONOject and JSONArray datatypes. However, I would like to switch to using classes generated via Parceler library. The classes generated with library's annotations can be wrapped into Parcels. It seems like a very convenient way of managing such communication. However, is there a way to convert Parceler's class or a Parcel class into a JSON string or JSONObject/Array?

GSON库支持的toJSON 的方法,我知道,改造做一些魔术包裹,JSON和GSON。

GSON library supports toJson method and I know that Retrofit does some magic with Parcels, JSON, and GSON.

推荐答案

我想这会帮助你,

例如与getter和setter方法​​创建一个类

Create a class with getters and setters method for example

class A
{
int b;
void setb(int x){this.b = x;}
int getb(){return this.b}
}

不是你可以从这个类的对象创建JSON:

than you can create json from the object of this class:

new Gson().toJson(a)

或从JSON对象:

a = new Gson().fromJson(data, A.class);

这篇关于安卓:转换Parcelable到JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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