-1

I would like to know how to parse this specific JSON structure in java:

[
   {
      "name":"property2",
      "value":"ANOTHER VALUE"
   },
   {
      "name":"property1",
      "value":"ANOTHER VALUE"
   }
]

I mean: I know how to parse in Java that file, but I would like to know if there is any java annotation I could use in order to take it to the following structure:

public class CustomClass    {
    private List<Map<String, String>> propertyListMap;
}

I asked this because I wanna avoid generating too much code...

2
  • I dont think this post is duplicated... Because no one has provided me with annotations that indicate the core framework to parse json file ina certain way... And, what I want to avoid, is the solution provided by Fouad Wahabi in hist last comment, because there is already a method which reads... Just wanna know how to instruct the framework classess... Commented Nov 22, 2016 at 13:59
  • Jarrod. This is what I dont wanna do... Because, as there is already a funcionality, I dont want to put specific and custom classes there... So that's why I'm asking if there are java annotations... Commented Nov 22, 2016 at 14:05

2 Answers 2

0

You can use Jackson , here's an example of how to parse a json file into a List<Map<String, Object>> :

   try {

        ObjectMapper mapper = new ObjectMapper();

        // read JSON from a file
        List<Map<String, Object>> lmap = mapper.readValue(
                new File("user.json"),
                new TypeReference<List<Map<String, Object>>>() {
        });
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
Sign up to request clarification or add additional context in comments.

Comments

0

Any specific reason you need a List<Map<String, String>>?

If not, I would go for a List<MappingClass> following this post's instructions: How to use Jackson to deserialise an array of objects

3 Comments

It's because I was assigned an automation test and have to make a get request which return this type of structure... And I would like to compare key's and values of the expected map and the one returned by the api call...
Then you should probably go with @Fouad Wahabi 's answer
Yes... But what happens is that, I want to know if there is a java annotation which I can use to indicate how to deserialize... There is already a functionality that reads the JSON structure... So I dont want to add more code than my Java class...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.