Map <String, HashSet <String>> in JSON and Pretty Print

I am trying to make my dataset fit this example:

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

I have Map<String, HashSet<String>>names and unique alpha-numeric values ​​corresponding to the specific objects to which these names can refer, and let these identifiers be input elements.

So, for example, Fyodor Mikhailovich Dostoyevskyit might have been associated with an identifier Q626, because it is a very specific reference, there are not many widely known numbers with this name. Whereas it Bushcan be attached to G027, Q290and Q118, possibly referring to, a person, beer and shrubs, in a certain order.

It looks like this (real much more):

[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);

, , ..

{"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"]
}

- ?


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;
    }
}
+2
2

, :

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, mapper.writeValue(file, listOfMyEntities),

[
  {
    "name": "some name here",
    "ids": ["id1", "id2", ...]  
  }
  // more elements here
]
+2

:

        String name_list_file = "/home/matthias/Workbench/SUTD/nytimes_corpus/NYTimesCorpus/2005/01/02/test/people_test.txt";

        String single_name;

        try (   
                // read in the original file, list of names, w/e
                InputStream stream_for_name_list_file = new FileInputStream( name_list_file );
                InputStreamReader stream_reader = new InputStreamReader( stream_for_name_list_file , Charset.forName("UTF-8"));
                BufferedReader line_reader = new BufferedReader( stream_reader );
            ) 
        {
            while (( single_name = line_reader.readLine() ) != null) 
            {
                //replace this by a URL encoder
                //String associated_alias = single_name.replace(' ', '+');
                String associated_alias = URLEncoder.encode( single_name , "UTF-8");

                String platonic_key = single_name;
                System.out.println("now processing: " + platonic_key);

                Wikidata_Q_Reader.getQ( platonic_key, associated_alias );
            }
        }

        //print the struc
        Wikidata_Q_Reader.print_data();


    }
-1

Source: https://habr.com/ru/post/1585020/


All Articles