分析JSONified数组时出错

分析JSONified数组时出错,json,gson,Json,Gson,我有一个包含SpellingError对象数组的JSON字符串。 我使用Gson将其转换为拼写错误列表。 解析工作正常(或者至少DDoS不会抛出任何parsing错误)。然而,当我尝试遍历我希望的拼写错误列表时,我发现它们实际上是StringMaps,我得到了错误 “com.google.gson.internal.StringMap无法转换为SpellingError” 知道使用Gson从JSON字符串中提取对象数组的正确方法吗?我可以看到数组中有3个StringMap对象,它们代表我的3个对

我有一个包含SpellingError对象数组的JSON字符串。 我使用Gson将其转换为拼写错误列表。 解析工作正常(或者至少DDoS不会抛出任何parsing错误)。然而,当我尝试遍历我希望的拼写错误列表时,我发现它们实际上是StringMaps,我得到了错误 “com.google.gson.internal.StringMap无法转换为SpellingError”

知道使用Gson从JSON字符串中提取对象数组的正确方法吗?我可以看到数组中有3个StringMap对象,它们代表我的3个对象

这是违规代码

    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);
        }
    }
相反,我得到了一个解析异常 “应为BEGIN\u对象,但为BEGIN\u数组”

这是我的拼写错误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;
    }
}

您可以使用TypeToken告诉Gson,您希望在结果列表中包含哪种类型的对象。 替换你的线路

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

Type collectionType=new-TypeToken(){}.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();
        }

    }

}
import java.lang.reflect.Type;
导入java.util.List;
导入com.google.gson.gson;
导入com.google.gson.reflect.TypeToken;
类拼写错误
{
私有字符串上下文;
私有内部错误索引;
公共字符串getContext()
{
返回上下文;
}
公共void setContext(字符串上下文)
{
this.context=上下文;
}
public int getErrorIndex()
{
返回错误索引;
}
公共无效setErrorIndex(整数索引)
{
this.errorIndex=索引;
}
}
公共类TestGson{
私有静态Gson m_Gson=new Gson();
类数据包{
公共字符串fn;
公共对象数据;
}
类聊天包{
公共室内ID;
公共字符串消息;
}
公共静态void main(字符串[]args){
字符串拼写ErrorsJSON=“[{\”上下文\“:\”这是一个stmple字符串\“,\”错误索引\“:\”3\”,{\”上下文\“:\”这是一个stmple字符串2\”,\”错误索引\“:\”3\”,{\”上下文\“:\”这是一个stmple字符串3\“,”错误索引\“:”3\”;
列表拼写错误;
Type collectionType=new-TypeToken(){}.getType();
spellingErrors=m_gson.fromJson(spellingErrorsJSON,collectionType);
for(拼写错误拼写错误:拼写错误)
{
String spellingError=spellingError.getContext();
}
}
}

否,不起作用,获取上面列出的第二个错误…”java.lang.IllegalStateException:应为BEGIN_数组,但为字符串“:(请参阅上面编辑的答案-我包含了一个完整的测试类,该类正在为我工作。
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();
        }

    }

}