说我有一个对象,我将它的所有属性都存储为纯JSON。有没有一种方法来存储.getClass()值,以便能够检索它并获取原始对象?将字符串值转换为类型
例:
class foo
{
int test;
String classType;
}
//other class
foo demo = new foo();
demo.classType = demo.getClass().toString();
//I'm not entirely sure how to convert classType into a type I can use to parse back the data, like so:
foo demo2 = (demo.classType)jsonData;
由于这可能是一个有点宽泛的问题,我会接受的答案,只是告诉我上面有哪些是所谓的(如果你将一个搜索词)。
你只是试图在进程之间/网络之间传递一个对象?你有没有考虑过使一个对象可序列化? – dahui
它作为响应对象的一部分(作为有效载荷)传递,所以我不认为我可以使用可序列化。 – Gabrielus
使用'Serializable'作为有效载荷并没有什么本质的错误(尽管它有很多痛苦点使它不受欢迎)。 [协议缓冲区](https://developers.google.com/protocol-buffers/)是Google解决此问题的解决方案,如果您有[Gson](https://github.com/google/gson)是另一个有用的工具需要坚持使用JSON。 – dimo414