Gson从对象中制作出类似firebase的json [英] Gson make firebase-like json from objects

查看:147
本文介绍了Gson从对象中制作出类似firebase的json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去手动创建的数据列表,这些数据本地存储在数据库中。现在我想分析这些数据并将它们通过 import json 选项放入firebase dbs中,但是我得到的内容看起来不像firebase生成的json。



我得到的是:

  [
{
id:id_1 ,
text:一些文本
},
{
id:id_2,
text:some text2
$,
...
]

我想要的是像这样:

  {
id_1:{
text:some text,
id:id_1
},
id_2:{
text:some text2,
id:id_1
},
...
}

我的卡片类

  class卡{
私人字符串ID;
私人字符串文本;


$ / code>

检索资料

  //返回卡片列表
List< Card> cards = myDatabase.retrieveData();
Gson gson = new Gson();
String data = gson.toJson(cards);

所以我该如何实现这个(看起来对我而言)动态命名的json属性,看起来像那些由firebase产生的?

编辑:
我发现gson有可以改变字段名称的FieldNamingStrategy接口。但它看起来并不像我想要的那样动态。



EDIT2
我的临时修复只是覆盖toString()

  @Override 
public String toString(){
return\+ id +\:{+
\ text \:+\+ text +\,+
\id \:+\+ id +\, +
\type \:+\+ type +\+
'}';


解决方案

将每个对象存储为字符串而不是对象。

您需要序列化一个对象才能获取该数据,而不是列表。例如,使用Map

  List< Card> cards = myDatabase.retrieveData(); 
地图< Map< String,Object>> fireMap = new TreeMap<>();
int i = 1; (卡c:卡)
{
Map< String,Object> cardMap = new TreeMap<>();
cardMap.put(text,c.getText());
fireMap.put(id_+(i ++),cardMap);
}
Gson gson = new Gson();
字符串数据= gson.toJson(fireMap);


In the past a manually created as list of data, which was stored locally in a database. Now I would like to parse those data and put them through import json option into firebase dbs, but what I get doesn't look like firebase generated json.

What I get is:

[  
   {  
      "id":"id_1",
      "text":"some text"
    },
    {  
      "id":"id_2",
      "text":"some text2"
    },
    ...
]

what I want is something like this:

{  
   "id_1": {  
      "text":"some text",
      "id":"id_1"
    },
    "id_2":{  
      "text":"some text2",
      "id":"id_1"
    },
    ...
}

my Card class

class Card{
    private String id;
    private String text;

}

retrieving data

//return list of card
List<Card> cards =myDatabase.retrieveData();
Gson gson = new Gson();
String data = gson.toJson(cards);

So how I can I achieve this (it looks to me) dynamically named properties for json that look like those generated by firebase ?

EDIT: I found that gson has FieldNamingStrategy interface that can change names of fields. But it looks to me it's not dynamic as I want.

EDIT2 my temp fix was to just override toString()

 @Override
 public String toString() {
     return "\""+id+"\": {" +
                     "\"text\":" + "\""+text+"\","+
                     "\"id\":" +"\""+ id +"\","+
                     "\"type\":"+ "\""+ type +"\""+
             '}';
    }

解决方案

Your "temp fix" will store each object as a string not a object.

You need to serialize an object to get that data, not a list.

For example, using a Map

List<Card> cards = myDatabase.retrieveData();
Map<Map<String, Object>> fireMap = new TreeMap<>();
int i = 1;
for (Card c : cards) {
    Map<String, Object> cardMap = new TreeMap<>();
    cardMap.put("text", c.getText());
    fireMap.put("id_" + (i++), cardMap);
}
Gson gson = new Gson();
String data = gson.toJson(fireMap);

这篇关于Gson从对象中制作出类似firebase的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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