一尘不染

地图>到JSON和漂亮的打印

json

我试图使我的数据集与此示例相对应:

var family = [{
    "name" : "Jason",
    "age" : "24",
    "gender" : "male"
},
{
    "name" : "Kyle",
    "age" : "21",
    "gender" : "male"
}];

我有一个Map<String, HashSet<String>>名称和与那些名称可以引用的特定实体相对应的唯一字母数字值,我们将这些条目称为“ ID”。

因此,例如,Fyodor Mikhailovich Dostoyevsky可能与ID有关Q626,因为这是一个非常具体的参考,因此没有很多广为人知的具有该名称的图形。然而,Bush可能被附加到G027Q290Q118,引用也许是男人,啤酒和灌木,没有特定的顺序。

看起来像这样(实际的更大):

[Rao=[Q7293658, , Q7293657, Q12953055, Q3531237, Q4178159, Q1138810, Q579515, Q3365064, Q7293664, Q1133815], Hani Durzy=[], Louise=[, Q1660645, Q130413, Q3215140, Q152779, Q233203, Q7871343, Q232402, Q82547, Q286488, Q156723, Q3263649, Q456386, Q233192, Q14714149, Q12125864, Q57669, Q168667, Q141410, Q166028], Reyna=[Q7573462, Q2892895, Q363257, Q151944, Q3740321, Q2857439, Q1453358, Q7319529, Q733716, Q16151941, Q7159448, Q5484172, Q6074271, Q1753185, Q7319532, Q5171205, Q3183869, Q1818527, Q251862, Q3840414, Q5271282, Q5606181]]

使用杰克逊,我这样尝试:

Map<String, HashSet<String>>  map = q_valMap;
mapper.writeValue(new File("JSON_Output/user.json"), map);

但这似乎是错误的,因为我的输出全部混杂在一起,即

{"Rao":["Q7293658","","Q7293657","Q12953055","Q3531237","Q4178159","Q1138810","Q579515","Q3365064","Q7293664","Q1133815"],"Hani Durzy":[""],"Louise":["","Q1660645","Q130413","Q3215140","Q152779","Q233203","Q7871343","Q232402","Q82547","Q286488","Q156723","Q3263649","Q456386","Q233192","Q14714149","Q12125864","Q57669","Q168667","Q141410","Q166028"],"Reyna":["Q7573462","Q2892895","Q363257","Q151944","Q3740321","Q2857439","Q1453358","Q7319529","Q733716","Q16151941","Q7159448","Q5484172","Q6074271","Q1753185","Q7319532","Q5171205","Q3183869","Q1818527","Q251862","Q3840414","Q5271282","Q5606181"]}

我是否需要迭代填充此JSON对象?


像上面的示例一样,我认为它应该看起来像这样,尽管下面只是伪鳕鱼的描述,也就是说,不完全是这个,而是类似的:

{
    key: "Rao"
    value:  ["Q7293658","","Q7293657","Q12953055","Q3531237","Q4178159","Q1138810","Q579515","Q3365064","Q7293664","Q1133815"]

    key: "Hani Durzy"
    value: [""]

    key: "Louise"
    value: ["","Q1660645","Q130413","Q3215140","Q152779","Q233203","Q7871343","Q232402","Q82547","Q286488","Q156723","Q3263649","Q456386","Q233192","Q14714149","Q12125864","Q57669","Q168667","Q141410","Q166028"]

    key: "Reyna"
    value: ["Q7573462","Q2892895","Q363257","Q151944","Q3740321","Q2857439","Q1453358","Q7319529","Q733716","Q16151941","Q7159448","Q5484172","Q6074271","Q1753185","Q7319532","Q5171205","Q3183869","Q1818527","Q251862","Q3840414","Q5271282","Q5606181"]
}

那不对吗?


更新

public class JsonMapFileExample 
{
    public static void map(Map<String, HashSet<String>> q_valMap ) 
    {

        ObjectMapper mapper = new ObjectMapper();


        ArrayNode array = mapper.createArrayNode();
        for ( Entry entry: q_valMap.entrySet() ) 
        {
          ObjectNode node = mapper.createObjectNode()
              .put("name", entry.getKey())
              .put("ids", entry.getValue());
          array.add(node);
        }
        mapper.writeValue("/home/matthias/Workbench/SUTD/nytimes_corpus/wdtk-parent/wdtk-examples/JSON_Output/user.json", array);

    }
}


class MyEntity
{
    private String name;
    Set<String> value; // use names that you want in the result JSON

    //constructors
    public MyEntity() 
    {

    }
    public MyEntity(String name) 
    {
        this.name = name;
    }

    //getters
    public String getName() 
    {
        return this.name;
    }
    public Set<String>  getValue() 
    {
        return this.value;
    }

    //setters
    public void setName(String name) 
    {
        this.name = name;
    }
    public void setValue(Set<String> value) 
    {
        this.value = value;
    }
}

阅读 231

收藏
2020-07-27

共1个答案

一尘不染

您可以手动设置密钥名称,例如:

ArrayNode array = mapper.createArrayNode();
for (Entry entry: yourMap.entries()) {
  ObjectNode node = mapper.createObjectNode()
      .put("name", entry.key())
      .putPOJO("ids", entry.value());
  array.add(node);
}
mapper.writeValue(file, array);

或者,您可以为数据创建一个类

class MyEntity {
  String name;
  Set<String> ids; // use names that you want in the JSON result
  // getters, setters if necessary
}

将您的数据映射转换为的列表MyEntity,然后使用Jackson ObjectMapper创建JSON like
mapper.writeValue(file, listOfMyEntities),输出结果将像

[
  {
    "name": "some name here",
    "ids": ["id1", "id2", ...]  
  }
  // more elements here
]
2020-07-27