简体   繁体   中英

get objects that json file contains with jackson and java

I have a JSON file like that:

{"city":[  
  {  
     "id":"c1",
     "name":"Paris",
     "population":2125851,
     "département":"075"
  },
  {  
     "id":"c2",
     "name":"Marseille",
     "population":797491,
     "département":"013"
  }],"person":[  
  {  
     "id":"p1",
     "name":"Jules Verne",
     "born":"c6",
     "category":"cat3",
     "birthdate":"1828-02-08"
  },
  {  
     "id":"p2",
     "name":"René Coty",
     "born":"c11",
     "category":"cat2",
     "birthdate":"1882-03-20"
  }]}

And I need to extract the objects names, so I need to get here "city" and "person". I tried with jackson some thing like that:

JsonParser jParser = null;
            JsonFactory jfactory = new JsonFactory();
            jParser = jfactory.createJsonParser(new File("graph.gjson"));
            while (jParser.nextToken() != JsonToken.END_OBJECT) {

                  String fieldname = jParser.getCurrentName();
                  jParser.nextToken();
                  System.out.println(jParser.getText());

But this doesn't work, any one can help me please?

I just found the solution

Object obj;
                try {
                    obj = parser.parse(new FileReader(json));
                    JSONObject jsonObject = (JSONObject) obj;
                    Set<String> keys = jsonObject.keySet();
                    java.util.Iterator<String> key= keys.iterator();
                    while(key.hasNext())
                    {    Object element = key.next();
                        System.out.println(element);
                    }



                } catch (IOException | org.json.simple.parser.ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

I wanted to share it with you for those who will search same thing like me, so the output here is gonna be the objects names "keys" which are city and person.

update your while loop condition to while (jParser.nextToken() != JsonToken.END_ARRAY) this will let you parse through the whole array instead of just singleobject

Update a more general solution for parsing everything

    JsonToken token = jParser.nextToken() ;
    while (token != null && token != JsonToken.END_OBJECT)
    {

        String fieldname = jParser.getCurrentName();
        if (fieldname != null)
        {
            token = jParser.nextToken();
            System.out.println(jParser.getText());
        }
        token = jParser.nextToken();
        if (token == JsonToken.END_OBJECT)
            token = jParser.nextToken();

    }

You might want to take a look at JsonPath if you don't want to cast your JSONs to POJOs, just want to extract some values from it:

public static void main(String[] args) {
    final String json = ...;
    final List<String> personNames = JsonPath.read(json, "$.person[*].name");
    final List<String> cityNames = JsonPath.read(json, "$.city[*].name");

    System.out.println(personNames);
    System.out.println(cityNames);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM