I was wondering, is there any JSON library which enable me
- Get a key to value map, out from JSON String (JSONObject able to do this)
- Get a Java object, out from JSON String (Google Gson able to do this)
The following both print value
package jsontest;
import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;
/**
*
* @author yccheok
*/
public class Main {
public static class Me {
public String key;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws JSONException {
final String s = "{\"key\" : \"value\"}";
// Feature 1
final JSONObject jsonObject = new JSONObject("{\"key\" : \"value\"}");
System.out.println(jsonObject.getString("key"));
// Feature 2
Gson gson = new Gson();
Me me = gson.fromJson(s, Me.class);
System.out.println(me.key);
}
}
Currently, I have to use 2 different libraries to achieve the above JSON related task. Is there any libraries which able to do both 2 tasks?