6

I am trying to convert json from a text file into a java object.

I have tried both the jackson library, I put in the dependency and what not. My json file has both camel case and underscores, and that is causing an error when running my program. Here is the code that I used for when relating to the gson librar and it does not do anything, the output is the same with or without the code that I placed.

  java.net.URL url = this.getClass().getResource("/test.json");
          File jsonFile = new File(url.getFile());
          System.out.println("Full path of file: " + jsonFile);
try 
      {

         BufferedReader br = new BufferedReader(new FileReader("/test.json"));

         // convert the json string back to object
         DataObject obj = gson.fromJson(br, DataObject.class);

         System.out.println(obj);

      } catch (IOException e) 
      {
         e.printStackTrace();
      }

Now I also tried the jackson library. Here is the code i used

java.net.URL url = this.getClass().getResource("/test.json");
      File jsonFile = new File(url.getFile());
      System.out.println("Full path of file: " + jsonFile);

ObjectMapper mapper = new ObjectMapper();
       mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
       InputStream is = Test_Project.class.getResourceAsStream("/test.json");
       SampleDto testObj = mapper.readValue(is, SampleDto.class);
       System.out.println(testObj.getCreatedByUrl());

I am not sure what to do,

Dan W
  • 5,718
  • 4
  • 33
  • 44
Jack
  • 343
  • 2
  • 3
  • 5

3 Answers3

21

This simple example works like a charm:
DTOs

public class SampleDTO 
{
   private String name;
   private InnerDTO inner;
   // getters/setters
}

public class InnerDTO 
{
   private int number;
   private String str; 
   // getters/setters  
}  

Gson

  BufferedReader br = new BufferedReader(new FileReader("/tmp/test.json"));
  SampleDTO sample = new Gson().fromJson(br, SampleDTO.class);  

Jackson

  InputStream inJson = SampleDTO.class.getResourceAsStream("/test.json");
  SampleDTO sample = new ObjectMapper().readValue(inJson, SampleDTO.class);

JSON (test.json)

{
   "name" : "Mike",
   "inner": {
      "number" : 5,
      "str" : "Simple!"
   }
}
Ilya
  • 29,135
  • 19
  • 110
  • 158
2
public static void main(String args[]){

   ObjectMapper mapper = new ObjectMapper();

  /**
    * Read object from file
    */
   Person person = mapper.readValue(new File("/home/document/person.json"), Person.class);
   System.out.println(person);
}
Vinit Jordan
  • 313
  • 1
  • 5
  • 13
  • 6
    While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn, and apply that knowledge to their own code. You are also likely to have positive feedback from users in the form of upvotes, when the code is explained. – borchvm May 06 '20 at 08:31
0

A common way of getting both array of json in file or simply json would be

InputStream inputStream= Employee.class.getResourceAsStream("/file.json");
CollectionType collectionType = mapper.getTypeFactory().constructCollectionType(List.class, Employee.class);

List<Employee> lstEmployees = mapper.readValue(inputStream, collectionType);

The file.json needs to be placed in the resources folder. If your file only has a json block without json array square brackets [] , you can skip the CollectionType

InputStream inputStream= Employee.class.getResourceAsStream("/file.json");
Employee employee = mapper.readValue(inputStream, Employee.class);

Also refer here for original question from where I have drawn.

veritas
  • 378
  • 1
  • 6
  • 16