Convert JSON to Map

后端 未结 17 3085
天涯浪人
天涯浪人 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 10:53

    I do it this way. It's Simple.

    import java.util.Map;
    import org.json.JSONObject;
    import com.google.gson.Gson;
    
    public class Main {
        public static void main(String[] args) {
            JSONObject jsonObj = new JSONObject("{ \"f1\":\"v1\"}");
            @SuppressWarnings("unchecked")
            Map map = new Gson().fromJson(jsonObj.toString(),Map.class);
            System.out.println(map);
        }
    }
    

提交回复
热议问题