Convert JSON to Map

后端 未结 17 3210
天涯浪人
天涯浪人 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:06

    With google's Gson 2.7 (probably earlier versions too, but I tested 2.7) it's as simple as:

    Map map = gson.fromJson(json, Map.class);
    

    Which returns a Map of type class com.google.gson.internal.LinkedTreeMap and works recursively on nested objects.

提交回复
热议问题