-4

How to parse JSON like:

{
  "key1": {
    "subKey1" : {
      "field1": "value",
      "field2": "value"
    },
    "subKey2": {
      "field1": "value",
      "field2": "value"
    }
  },
  "key2": {
    "subKey1" : {
      "field1": "value",
      "field2": "value"
    },
    "subKey2": {
      "field1": "value",
      "field2": "value"
    }
  }
}  

to get Object structure like:

  // key*       subKey*
Map<String, Map<String, DataType>> map;


public class DataType {

    private String field1;
    private String field2;

}

If this structure is too complicated, how to parse just a map of DataType at least?

user2602807
  • 1,011
  • 2
  • 11
  • 19

2 Answers2

2

the solution is quite simple:

ObjectMapper jsonMapper = new ObjectMapper();
TypeReference<Map<String, Map<String, DataType>>> typeRef = 
        new TypeReference<Map<String, Map<String, DataType>>>() {};
Map<String, Map<String, DataType>> configMap = jsonMapper.readValue(strJson, typeRef);
user2602807
  • 1,011
  • 2
  • 11
  • 19
0

I have been dealing with this same issue. I've managed to get it to string then look for the string manually by using...

String string1 = "example of response";
string1.contains(response.body().getAsJsonObject().toString());

Another alternative would be to use regex (http://www.rexegg.com/regex-quickstart.html) to extract the string you want replacing characters like } and ] with a comma...

Hope this helps in some way. Sorry it's not very convenient