用Gson解析JSON地图/字典? [英] Parsing JSON maps / dictionaries with Gson?
问题描述
$ b
{key1:value1,
key2:value2,
key3:
{childKey1:childValue1,
childKey2:childValue2,
childKey3: childValue3}
}
class Egg {
@SerializedName(key1)
private String mKey1;
@SerializedName(key2)
private String mKey2;
@SerializedName(key3)
// ???
}
我正在阅读Gson文档,但无法弄清楚如何正确反序列化一个字典到一个地图。
Gson很容易处理带有名称:值对的JSON对象的反序列化成Java Map
。
以下是使用原始问题中的JSON的一个示例。 (这个例子还演示了如何使用 FieldNamingStrategy
来避免为每个字段指定序列化名称,前提是字段到元素的名称映射是一致的。)
import java.io.FileReader;
import java.lang.reflect.Field;
import java.util.Map;
import com.google.gson.FieldNamingStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
$ b $ public class Foo
{
public static void main(String [] args)throws Exception
{
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setFieldNamingStrategy(new MyFieldNamingStrategy());
Gson gson = gsonBuilder.create();
egg egg = gson.fromJson(new FileReader(input.json),Egg.class);
System.out.println(gson.toJson(egg));
}
}
class Egg
{
private String mKey1;
private String mKey2;
私人地图< String,String> mKey3;
}
class MyFieldNamingStrategy实现FieldNamingStrategy
{
//将Java字段名转换为其JSON元素名称表示形式。
@Override
public String translateName(Field field)
{
String name = field.getName();
char newFirstChar = Character.toLowerCase(name.charAt(1));
返回newFirstChar + name.substring(2);
}
}
I need to parse a JSON Response that looks like:
{"key1": "value1",
"key2": "value2",
"key3":
{"childKey1": "childValue1",
"childKey2": "childValue2",
"childKey3": "childValue3" }
}
class Egg {
@SerializedName("key1")
private String mKey1;
@SerializedName("key2")
private String mKey2;
@SerializedName("key3")
// ???
}
I'm reading through the Gson docs but cannot figure out how to properly deserialize a dictionary to a Map.
Gson readily handles deserialization of a JSON object with name:value pairs into a Java Map
.
Following is such an example using the JSON from the original question. (This example also demonstrates using a FieldNamingStrategy
to avoid specifying the serialized name for every field, provided that the field-to-element name mapping is consistent.)
import java.io.FileReader;
import java.lang.reflect.Field;
import java.util.Map;
import com.google.gson.FieldNamingStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class Foo
{
public static void main(String[] args) throws Exception
{
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setFieldNamingStrategy(new MyFieldNamingStrategy());
Gson gson = gsonBuilder.create();
Egg egg = gson.fromJson(new FileReader("input.json"), Egg.class);
System.out.println(gson.toJson(egg));
}
}
class Egg
{
private String mKey1;
private String mKey2;
private Map<String, String> mKey3;
}
class MyFieldNamingStrategy implements FieldNamingStrategy
{
//Translates the Java field name into its JSON element name representation.
@Override
public String translateName(Field field)
{
String name = field.getName();
char newFirstChar = Character.toLowerCase(name.charAt(1));
return newFirstChar + name.substring(2);
}
}
这篇关于用Gson解析JSON地图/字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!