0

I'm new to this, and I want to read JSON file imported with rest api and parse it with spring boot. I worked with CSV file with his method :

 @RequestMapping(value = "/import", method = RequestMethod.POST)
      public String handleFileUpload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
          String name = multipartFile.getOriginalFilename();
          System.out.println("File name: "+name);
          byte[] bytes = multipartFile.getBytes();
          System.out.println("File uploaded content:\n" + new String(bytes));
          return "file uploaded";
      }

Now i want to parse a JSON File :

[  
    {  
        "name":"John",
        "city":"Berlin",
        "cars":[  
            "audi",
            "bmw"
        ],
        "job":"Teacher"
    },
    {  
        "name":"Mark",
        "city":"Oslo",
        "cars":[  
            "VW",
            "Toyata"
        ],
        "job":"Doctor"
    }
]

I have try it to parse this file with java and it works for me but I dont know how to get it with rest api

This method to parse te file JSON and it works

public static void main(String[] args) throws FileNotFoundException,
    IOException, ParseException {

JSONParser parser = new JSONParser();
JSONArray jsonArray = (JSONArray) parser.parse(new FileReader(
        "D:/PFE 2018/testjsonfile.json"));

for (Object o : jsonArray) {
    JSONObject person = (JSONObject) o;

    String strName = (String) person.get("name");
    System.out.println("Name::::" + strName);

    String strCity = (String) person.get("city");
    System.out.println("City::::" + strCity);

    JSONArray arrays = (JSONArray) person.get("cars");
    for (Object object : arrays) {
        System.out.println("cars::::" + object);
    }
    String strJob = (String) person.get("job");
    System.out.println("Job::::" + strJob);
    System.out.println();

}

}

now how to reuse this methode with rest api

0

1 Answer 1

1

It depends what you want to do with your JSON (it's not entirely clear in your question). In general, good practice with Spring Boot is to use Jackson either:

  • binding your JSON with a POJO if you expect your JSON to have a known format or

  • mapping your JSON in a tree.

Examples for the described behaviours can be found in this article for instance.

Sign up to request clarification or add additional context in comments.

4 Comments

thank you, I want to read this JSON file, parse it to an object to inject it in the database
Ok, so my answer is what you're looking for as practice. If you have already a Java object then it's normally a POJO and you want the first method.
Can I do it without POJO class ? i already parse it you can show it in the question i update it but nnow i want to insert the file with rest api multipart file and then parse it ?
Alright, then instead of using import, etc... You can get the object from the REST call and use Jackson to convert it into a tree (method 2),that won't require a POJO and you'll have a tree object easily parseable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.