Convert JSON to Map

后端 未结 17 3196
天涯浪人
天涯浪人 2020-11-22 10:20

What is the best way to convert a JSON code as this:

{ 
    \"data\" : 
    { 
        \"field1\" : \"value1\", 
        \"field2\" : \"value2\"
    }
}
         


        
17条回答
  •  醉话见心
    2020-11-22 11:15

    I like google gson library.
    When you don't know structure of json. You can use

    JsonElement root = new JsonParser().parse(jsonString);
    

    and then you can work with json. e.g. how to get "value1" from your gson:

    String value1 = root.getAsJsonObject().get("data").getAsJsonObject().get("field1").getAsString();
    

提交回复
热议问题