-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...

  • 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... – Agustn Ernesto Cardeilhac Bans Nov 22 '16 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... – Agustn Ernesto Cardeilhac Bans Nov 22 '16 at 14:05

2 Answers2

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();
        }
Fouad Wahabi
  • 804
  • 7
  • 16
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

Community
  • 1
  • 1
Manuel S.
  • 411
  • 8
  • 21
  • 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... – Agustn Ernesto Cardeilhac Bans Nov 22 '16 at 12:51
  • Then you should probably go with @Fouad Wahabi 's answer – Manuel S. Nov 22 '16 at 12:54
  • 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... – Agustn Ernesto Cardeilhac Bans Nov 22 '16 at 13:21