Java Json Parse getMapFromGson(String json)

Here you can find the source of getMapFromGson(String json)

Description

get Map From Gson

License

Apache License

Declaration

public static Map<String, String> getMapFromGson(String json) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Type;
import java.text.DateFormat;
import java.util.Date;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;

public class Main {
    private static final Gson G = new GsonBuilder().setDateFormat(DateFormat.LONG)
            .registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
                public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                        throws JsonParseException {
                    return new Date(json.getAsJsonPrimitive().getAsLong());
                }/*from   ww  w.ja va 2  s .  c  o  m*/
            }).create();

    public static Map<String, String> getMapFromGson(String json) {
        Type type = new TypeToken<Map<String, String>>() {
        }.getType();
        return G.fromJson(json, type);
    }
}

Related

  1. json2Obj(String json, Class clazz)
  2. jsonString(String s)
  3. parse(JSONObject jsonobj)
  4. parse(String json, Class tClass)