Map< String,HashSet< String>>到JSON,&漂亮的打印 [英] Map<String, HashSet<String>> to JSON, & Pretty Print
问题描述
var family = [{
姓名:贾森,
年龄:24,
性别:男性
},
{
姓名凯尔,
年龄:21,
性别:男性
}];
我有一个 Map< String,HashSet< String>> code>名称和与这些名称可引用的特定实体相对应的唯一字母数字值,我们称这些条目为ID。因此,例如,
Fyodor Mikhailovich Dostoyevsky
可能与ID Q626有关。 / code>,因为这是一个非常具体的参考,并没有很多这个名字广为人知的人物。然而,
Bush
可能附加于 G027
, Q290
,和 Q118
,并没有特别的顺序参考男人,啤酒和灌木。
看起来像这样(真正的更大):
<$ c $ [Rao = [Q7293658,Q7293657,Q12953055,Q3531237,Q4178159,Q1138810,Q579515,Q3365064,Q7293664,Q1133815],Hani Durzy = [],Louise = [,Q1660645,Q130413,Q3215140,Q152779,Q233203,Q7871343, Q234029,Q1353439,Q143358,Q7319529,Q733716,Q16151941,Q237328,Q156723,Q3263649,Q456386,Q233192,Q14714149,Q12125864,Q57669,Q168667,Q141410,Q166028] Q7159448,Q5484172,Q6074271,Q1753185,Q7319532,Q5171205,Q3183869,Q1818527,Q251862,Q3840414,Q5271282,Q5606181]]
使用Jackson我试过像这样:
Map< String,的HashSet<字符串>> map = q_valMap;
mapper.writeValue(new File(JSON_Output / user.json),map);
但这似乎是错误的,因为我的输出全部混杂在一起,即
{Rao:[Q7293658,,Q7293657,Q12953055,Q3531237,Q4178159,Q1138810, Q579515, Q3365064, Q7293664, Q1133815], 哈尼杜尔责:[ ], 路易丝:[ , Q1660645, Q130413, Q3215140, Q152779 Q233203, Q7871343, Q232402, Q82547, Q286488, Q156723, Q3263649, Q456386, Q233192, Q14714149, Q12125864, Q57669, Q168667\" , Q141410, Q166028], 雷纳:[ Q7573462, Q2892895, Q363257, Q151944, Q3740321, Q2857439, Q1453358, Q7319529, Q733716\" , Q16151941, Q7159448, Q5484172, Q6074271, Q1753185, Q7319532, Q5171205, Q3183869, Q1818527, Q251862, Q3840414, Q5271282 ,Q5606181]}
我是否需要迭代填充此JSON对象?
就像上面的例子一样,我认为它应该看起来像这样,尽管接下来只是一种伪科学表征,也就是说,n不是这个,而是类似的东西:
{
key:Rao
value:[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]
}
是不是正确?
更新
public class JsonMapFileExample
{
p ublic 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);
$ b class MyEntity
{
private String name;
Set< String>值; //在结果中使用你想要的名字JSON
//构造函数
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;
$ div $解析方案
你可以手动设置键名,例如:
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; //在JSON结果中使用你想要的名字
// getters,setters(如果需要的话)
}
将您的数据映射转换为 MyEntity
列表,然后使用Jackson ObjectMapper
创建JSON mapper.writeValue(file,listOfMyEntities)
,输出结果会像
[
{
name:some name here,
ids:[id1,id2,...]
}
//更多元素在这里
]
I'm trying to make my dataset correspond to this example:
var family = [{
"name" : "Jason",
"age" : "24",
"gender" : "male"
},
{
"name" : "Kyle",
"age" : "21",
"gender" : "male"
}];
I have a Map<String, HashSet<String>>
of Names and unique alpha-numeric values correponding to specific entities to which those names could refer, let's call these entry items "IDs".
So for instance, Fyodor Mikhailovich Dostoyevsky
would perhaps be related to the ID Q626
, because that's a very specific reference, there aren't many widely known figures with that name. Whereas, Bush
might be attached to G027
, Q290
, and Q118
, referencing perhaps the man, the beer, and the shrub, in no particular order.
It looks like this (the real one is much bigger):
[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]]
Using Jackson I tried like this:
Map<String, HashSet<String>> map = q_valMap;
mapper.writeValue(new File("JSON_Output/user.json"), map);
But this seems wrong, as my output was all jumbled together, i.e.
{"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"]}
Do I just have to populate this JSON object iteratively?
Like the example up top, I think it should look something like this, though what follows is only a pseudocodish characterization, which is to say, not exactly this but something similar:
{
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"]
}
is that not right?
UPDATE
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;
}
}
You could manually set the key names, something like:
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);
Alternatively, you could create a class for your data
class MyEntity {
String name;
Set<String> ids; // use names that you want in the JSON result
// getters, setters if necessary
}
Transform your data map into a list of MyEntity
, then use Jackson ObjectMapper
to create JSON like mapper.writeValue(file, listOfMyEntities)
, the output would be like
[
{
"name": "some name here",
"ids": ["id1", "id2", ...]
}
// more elements here
]
这篇关于Map< String,HashSet< String>>到JSON,&漂亮的打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!