0

How to convert file JSON to class POJO complet using Java?

I tried with Jackson API but the result is arrayList and I want a class complet like:

class test {
    public int age ;
    public string name ;
}

This is my code:

public static void main(String[] a) 
{ 
    ObjectMapper mapper = new ObjectMapper();

    Staff value = null;
    try {
        value = mapper.readValue(new File("C://staff.json"), Staff.class);
    } catch (Exception e) {
        e.printStackTrace();
    }   

    System.out.println(value);                  
}

Staff is a normal class with fields and getters setters.

I want to display the class complet when I enter file json, not display array.

Léa Gris
  • 17,497
  • 4
  • 32
  • 41
  • Could you please give us the input JSON, the Staff class and what is printed by your println() call ? – Thomas Martin Sep 03 '19 at 13:56
  • Possible duplicate of [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – kAliert Sep 03 '19 at 14:28
  • @ThomasMartin this is json File { "name": "mkyong", "age": 38, "position": [ "Founder", "CTO", "Writer" ], "skills": [ "java", "python", "node", "kotlin" ], "salary": { "2018": 14000, "2012": 12000, "2010": 10000 } } and my println Staff[name=mkyong,age=38,position=[Founder, CTO, Writer],skills=[java, python, node, kotlin],salary={2018=14000, 2012=12000, 2010=10000}] thank's again – Lamiae Dev Sep 03 '19 at 16:13
  • The println is simply printing what you have in a toString() method in Staff class. The values are correctly read from JSON, as per your comments. Please check that method and modify based on your requirements. When you say "I want to display the class complet", what is it that you want to display? – MRTJ Sep 03 '19 at 19:29
  • @MRTJ yes i know the response is correctly , i mean i want to generate dynamically the class POJO like public class X { public string name ; – Lamiae Dev Sep 03 '19 at 20:06
  • Why do you want to do that ? What's the point ? Besides, Java is a statically-typed language, so it's not possible by design. – Thomas Martin Sep 04 '19 at 08:03

0 Answers0