tags:

views:

67

answers:

2

I was wondering, is there any JSON library which enable me

  1. Get a key to value map, out from JSON String (JSONObject able to do this)
  2. 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?

+2  A: 

You can use Jackson.

It has a databinding solution (like Gson) and a tree model view (like JSONObject)

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;

public class Main {

    public static class Me {
        public String key;
    }

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        String json = "{\"key\" : \"value\"}";

        // Feature 1
        JsonNode rootNode = mapper.readValue(json, JsonNode.class);
        System.out.println(rootNode.get("key").getTextValue());

        // Feature 2
        Me value = mapper.readValue(json, Me.class);
        System.out.println(value.key);
    }
}
Colin Hebert
For feature 1, is it OK for me to `HashMap<String,Object> untyped = mapper.readValue(json, HashMap.class);`. May I know which method is better?
Yan Cheng CHEOK
No, not really, as JSon elements can have other sub elements, the JsonNode system is way better.
Colin Hebert
But...but...but... http://pastebin.com/CRUH4dyG I try. It does support sub elements. Is that what you mean?
Yan Cheng CHEOK
Hum my bad, it seems to work with Map, List and String arrays. `Map<String,Object> userData = mapper.readValue(json, Map.class);` it's considered as simple databinding.
Colin Hebert
Thanks for recommending this library. I am very happy with it :)
Yan Cheng CHEOK
A: 

I'm not sure if Gson has some easier built in way to do this, but this seems to work:

public enum JsonElementDeserializer implements JsonDeserializer<JsonElement> {
  INSTANCE;

  public JsonElement deserialize(
      JsonElement json, Type typeOfT, JsonDeserializationContext context) 
      throws JsonParseException {
    return json;
  }
}

And then:

Gson gson = new GsonBuilder().registerTypeAdapter(JsonElement.class,
    JsonElementDeserializer.INSTANCE).create();
JsonElement rootElement = gson.fromJson(reader, JsonElement.class);
ColinD