2012-11-13 60 views
0

我有一個包含SpellingError對象數組的JSON字符串。 我使用Gson將其轉換爲SpellingErrors列表。 解析工作正常(或至少不會拋出任何parsinig錯誤)。但是,當我嘗試迭代我希望的SpellingErrors列表時,我發現它們實際上是StringMaps,並且出現錯誤 「com.google.gson.internal.StringMap無法投射到SpellingError」解析JSON化數組時出錯

任何想法什麼是正確的方式來使用Gson從JSON字符串中拉出我的數組對象?我可以看到數組中有3個表示我的3個對象的StringMap對象。

這裏是有問題的代碼

spellingErrorsJSON = "[{\"context\":\"This is a stmple string\",\"errorIndex\":\"3\"},{\"context\":\"This is a stmple string 2\",\"errorIndex\":\"3\"},{\"context\":\"This is a stmple string 3\",\"errorIndex\":\"3\"}]"; 
    List<SpellingError>spellingErrors; 
    spellingErrors = m_gson.fromJson(spellingErrorsJSON, List.class); 

    for(SpellingError spellingError : spellingErrors) 
    { 
     if(spellingError.isValid()) 
     { 
      String spellingMistake = spellingError.getSpellingMistake(); 
      String[] suggestions = Query.lookupSpelling(spellingMistake, LOCALE_US); 
     } 
    } 

但是,如果我用

m_gson.fromJson(spellingErrorsJSON, SpellingError[].class); 

代替然後我得到一個解析異常 「預期BEGIN_OBJECT但BEGIN_ARRAY」

這裏是我的SpellingError PoJo

public class SpellingError 
{ 
    private String [] context; 
    private int errorIndex; 

    public String[] getContext() 
    { 
     return context; 
    } 

    public void setContext(String[] context) 
    { 
     this.context = context; 
    } 

    public int getErrorIndex() 
    { 
     return errorIndex; 
    } 

    public void setErrorIndex(int index) 
    { 
     this.errorIndex = index; 
    } 
} 

回答

0

您可以使用TypeToken來告訴Gson,您想要在結果列表中包含哪種類型的對象。 更換你行

spellingErrors = m_gson.fromJson(spellingErrorsJSON, List.class); 

Type collectionType = new TypeToken<List<SpellingError>>(){}.getType(); 
spellingErrors = m_gson.fromJson(spellingErrorsJSON, collectionType); 

編輯:這是爲我工作:

import java.lang.reflect.Type; 
import java.util.List; 

import com.google.gson.Gson; 
import com.google.gson.reflect.TypeToken; 

class SpellingError 
{ 
    private String context; 
    private int errorIndex; 

    public String getContext() 
    { 
     return context; 
    } 

    public void setContext(String context) 
    { 
     this.context = context; 
    } 

    public int getErrorIndex() 
    { 
     return errorIndex; 
    } 

    public void setErrorIndex(int index) 
    { 
     this.errorIndex = index; 
    } 
} 


public class TestGson { 
    private static Gson m_gson = new Gson(); 

    class DataPacket { 
     public String fn; 
     public Object data; 
    } 

    class ChatPacket { 
     public int roomId; 
     public String message; 
    } 


    public static void main(String[] args) { 
     String spellingErrorsJSON = "[{\"context\":\"This is a stmple string\",\"errorIndex\":\"3\"},{\"context\":\"This is a stmple string 2\",\"errorIndex\":\"3\"},{\"context\":\"This is a stmple string 3\",\"errorIndex\":\"3\"}]"; 
     List<SpellingError>spellingErrors; 
     Type collectionType = new TypeToken<List<SpellingError>>(){}.getType(); 
     spellingErrors = m_gson.fromJson(spellingErrorsJSON, collectionType); 


     for(SpellingError spellingError : spellingErrors) 
     { 
      String spellingMistake = spellingError.getContext(); 
     } 

    } 

} 
+0

沒有,沒有工作,讓我上面列出的第二個錯誤... 「java.lang.IllegalStateException:預計BEGIN_ARRAY,但是STRING」:( – MayoMan

+0

請參閱我上面編輯的答案 - 我包括一個完整的測試類,爲我工作。 –