How to efficiently map a org.json.JSONObject to a POJO?

前端 未结 4 2160
清歌不尽
清歌不尽 2020-12-05 00:28

This question must have been asked before, but I couldn\'t find it.

I\'m using a 3rd party library to retrieve data in JSON format. The library offers the data to me

4条回答
  •  温柔的废话
    2020-12-05 01:03

    If you're not tied with Jackson, you can use the handy google-gson library as an alternative. It requires only one jar and is very simple to use:

    Converting a java object into a JSON string:

      String json_string = new Gson().toJson(an_object);
    

    Creating a java object from a JSON string:

      MyObject obj = new Gson().fromJson(a_json_string, MyObject.class);
    

    I dont't know about performance compared to Jackson, but it's hard to be simpler than this... Gson is a stable and widely used library.

    See https://code.google.com/p/google-gson/

提交回复
热议问题