如何将类型为 (Object obj) 的对象转换为 JSON,然后再转换回来?

huangapple go评论157阅读模式
英文:

How to convert (Object obj) this type of object to JSON and then back?

问题

我尝试了这个:

Gson gson = new Gson();
Log.e("TAGS", "已知人脸额外对象:" + rec.getExtra());
Log.e("TAGS", "已知人脸额外JSON:" + gson.toJson(rec.getExtra()));
Object obj = gson.fromJson(gson.toJson(rec.getExtra()), Object.class);
Log.e("TAGS", "从JSON解析的已知人脸额外对象:" + obj);

我得到的响应是:

已知人脸额外对象:[[F@1f4af8f

已知人脸额外JSON:[[0.0010238411,-0.004055016]]

从JSON解析的已知人脸额外对象:[[0.0010238411, -0.004055016]]

我不确定为什么它无法转换回我所需的对象!

英文:

I tried this:

Gson gson = new Gson();
            Log.e("TAGS", "Known Face extra obj: " + rec.getExtra());
            Log.e("TAGS", "Known Face extra JSON: " + gson.toJson(rec.getExtra()));
            Object obj = gson.fromJson(gson.toJson(rec.getExtra()), Object.class);
            Log.e("TAGS", "Known Face extra from JSON: " + obj);

The response what I get is:

Known Face extra obj: [[F@1f4af8f

Known Face extra JSON: [[0.0010238411,-0.004055016]]

Known Face extra from JSON: [[0.0010238411, -0.004055016]]

I'm not sure why it is not converting back to the Object that I need!

答案1

得分: 1

也许你可以尝试在fromJson中指定类型,像这样:

Object obj = gson.fromJson(gson.toJson(rec), double[][].class);
英文:

Maybe you can try to specify the type in fromJson like

Object obj = gson.fromJson(gson.toJson(rec), double[][].class);

huangapple
  • 本文由 发表于 2020年8月24日 15:22:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63556424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定