How to get JSON file

I have been trying to get the json file as a POJO but I am not able to. Getting error with deserialization.
I have got the json as a string in a variable and used it to map to the pojo after creating a class with the required variabels, getter and setter methods under a try-catch block. As the json is an array format I created a array of the class and tried using objectMapper but to no success.
Getting the following error :

com.fasterxml.jackson.databind.JsonMappingException: Cannot deserialize Class com.crio.warmup.stock.PortfolioManagerApplication$1Stocks (of type local/anonymous) as a Bean
 at [Source: (String)"[
  {
    "symbol": "AAPL",
    "quantity": 100,
    "tradeType": "BUY",
    "purchaseDate": "2019-01-02"
  },
  {
    "symbol": "MSFT",
    "quantity": 10,
    "tradeType": "BUY",
    "purchaseDate": "2019-01-02"
  },
  {
    "symbol": "GOOGL",
    "quantity": 50,
    "tradeType": "BUY",
    "purchaseDate": "2019-01-02"
  }
]"

I have been trying to get this to work for 3 days but I am not able to get this to work so I am posting this.

use pojo class, google about POJO

So the part where you created an array of the class is correct.
Now try using the concept of loop for that array

Already mentioned that I’m using POJO

How should the loop run and the objectmapper work? I am trying to run a simple for loop and using objectmapper passing the entire json string but that doesn’t seem to do the job

You have an array of json objects right now, you just need to iterate through it. Consider using a ‘for’ loop through the array. For reference look into the structure of the POJO you’re using, everything will be clear onwards.

you are getting an error bcz you are tyring to convert an array of objects to an object
Try adjusting the objectmapper method arguments (you can even do it without loops )
& You don’t need to create pojos as they are already in preset in dto directory

1 Like

Please try searching for how to deserialize an array of JSON objects you’ll find what you want.

Try using for loop for array of objects and then use getter of symbol for every iteration