0

我正在尝试使用来自 Android 的 Json 来使用列表。但是,我认为我没有很好地生成列表,当我尝试用 Json 解析时,我得到了错误。

我得到这个结果:

[{"id":7,"word":"w6","definition":"d6","language":"es","numberDefinition":0,"success":false},{"id": 8,"word":"w7\n","definition":"d7","language":"es","numberDefinition":0,"success":false},{"id":9,"word ":"w8","definition":"d8","language":"es","numberDefinition":0,"success":false}]

@GET
@Path("words")
@Produces(MediaType.APPLICATION_JSON)
public List<Word> getWords(@QueryParam("number") int number  ) {
    List<Word> list = null;

    list = dictionary.getListWords(number);

    return list;
}

我在 Android 中的 java 代码是:

            respJSON = new JSONObject(response);
            JSONArray lineItems = respJSON.getJSONArray("words");
            listWords = new ArrayList<Word>();

            for(int i=0; i<lineItems.length(); i++)
            {               
                JSONObject obj = lineItems.getJSONObject(i);                
                id = obj.getInt(ConstantsTrivial.WORD_ID);
                definition = obj.getString(ConstantsTrivial.WORD_DEFINITION);
                language = obj.getString(ConstantsTrivial.WORD_LANGUAGE);
                wordName = obj.getString(ConstantsTrivial.WORD_NAME);
                numberDefinition = obj.getInt(ConstantsTrivial.WORD_NUMBER_DEFINITION);
                word = new Word(id, wordName, definition, language, numberDefinition);

                listWords.add(word);
            }

我正在寻找一个“单词”,我知道这个错误,但我不知道我应该怎么做才能让我的网络服务以正确的方式生成列表..

错误是:

03-19 13:09:48.086: E/WebServiceTask(9868): 值 [{"numberDefinition":0,"id":1,"word":"w0","language":"ES","definition" :"d0","success":false},{"numberDefinition":0,"id":3,"word":"w2","language":"es","definition":"d2","成功":false},{"numberDefinition":0,"id":4,"word":"w3","language":"es","definition":"d3","success":false}, {"numberDefinition":0,"id":9,"word":"w8","language":"es","definition":"d8","success":false}] 类型为 org.json。 JSONArray 无法转换为 JSONObject

谢谢你。

4

1 回答 1

0

错误出现在代码的第一行。您的值是 JSONArray,而不是 JSONObject。该行应该是:

JSONArray lineItems = new JSONArray(response);

并且不要忘记删除第二行。

于 2013-03-19T13:09:38.617 回答