![](/img/trans.png)
[英]Google Gson - com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT
[英]Gson: JsonSyntaxException: Expected BEGIN_OBJECT but was STRING
我正在嘗試將JSON字符串轉換為HashMap。 這是我的一些代碼:
public static void createSimpleAppointment(String json){
try
{
Gson gson = new Gson();
HashMap<String,String> data = new HashMap<String,String>();
data = gson.fromJson(json, data.getClass());
在最后一行(我設置數據的地方)拋出異常。
JSON字符串是
{“body”:“body”,“startDate”:“2014-05-30 11:00:00”,“endDate”:“2014-05-30 12:00:00”,“location”:“location” ,“主題”:“主題!”}
我得到的例外是
位於sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl)的sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法)中的線程“main”java.lang.reflect.InvocationTargetException中的異常.java:43)atg.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)中的java.lang.reflect.Method.invoke(Method.java:606)引起:com.google。 gson.JsonSyntaxException:java.lang.IllegalStateException:預期BEGIN_OBJECT但是在com.google.gson.Gson.fromJson(Gson.java:815)的com.google.gson.Gson.fromJson(Gson。)的第1行第3列是STRING。 java:768)com.google.gson.Gson.fromJson(Gson.java:717)at com.google.gson.Gson.fromJson(Gson.java:689)at ews.calendar.Calendar.createSimpleAppointment(Calendar.java) :70)在ews.main.gateway.Main.main(Main.java:34)... 5更多引起:java.lang.IllegalStateException:預期BEGIN_OBJECT但是在第1行第3列的STRING是com.g o.com.gson.stream.JsonReader.beginObject(JsonReader.java:374)位於com.google.gson.internal.bind的com.google.gson.internal.bind.MapTypeAdapterFactory $ Adapter.read(MapTypeAdapterFactory.java:183)。來自com.google.gson.Gson.fromJson的MapTypeAdapterFactory $ Adapter.read(MapTypeAdapterFactory.java:145)(Gson.java:803)... 10更多
您也可以將JSON字符串轉換為Java POJO類。 POJO類中實例成員的名稱應與JSON字符串中定義的完全相同( 區分大小寫 )。
默認情況下,您不需要將日期字符串轉換為由GsonBuilder#setDateFormat()方法處理的日期對象。
示例代碼:
class MyJSONObject {
private String body;
private Date startDate;
private Date endDate;
private String location;
private String subject;
// getter & setter
}
// convert json string to MyJSONObject
MyJSONObject data = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
.create().fromJson(jsonString, MyJSONObject.class);
// pretty printing of JSON string back from POJO class object
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(data));
輸出:
{
"body": "body",
"startDate": "May 30, 2014 11:00:00 AM",
"endDate": "May 30, 2014 12:00:00 PM",
"location": "location",
"subject": "subject!"
}
我沒有得到你用Gson 2.2.4報告的異常,但是你應該開始使用TypeToken
用於泛型類型。
HashMap<String, String> data = gson.fromJson(json, new TypeToken<HashMap<String, String>>() {}.getType());
該錯誤可能有點誤導 - 在我的情況下,它是LocalDateTime
缺少的Deserializer
,並且在第一個日期失敗。
我在一個類中有一個要被json-ed的Map和上面描述的相同的錯誤:
public class Setting implements Serializable {
private Map<Key, Double> keyToValue = new HashMap<>();
// other fields, setters and getters...
public Map<Key, Double> getKeyToValue() {
return keyToValue;
}
public void setKeyToValue(Map<PlantKey, Double> keyToValue) {
this.keyToValue = keyToValue;
}
}
我不得不使用以下內容使其工作:
GsonBuilder builder = new GsonBuilder();
builder.enableComplexMapKeySerialization();
builder.create();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.