0

我有一个这样的json字符串:

String result={[{"id":"2","fullname":"Course 1"},{"id":"3","fullname":"Course 2"}]}

在java中,我编写了这段代码来解码那个json字符串:

public class Courses {
public String id,name;  
}
Gson gs = new Gson();
Type listType = new TypeToken<List<Courses>>(){}.getType();
List<Courses> listCourses= gs.fromJson(result, listType);

但我总是收到错误:

06-09 04:21:11.614:E/AndroidRuntime(449):原因:java.lang.IllegalStateException:这不是 JSON 数组。

我错了什么?

谢谢 :)

4

2 回答 2

1

您的 json 无效。请使用一些 jsonvalidator 检查 json 格式是否正确。要检查 json 格式,请参阅链接

JSON格式

要从 json 中获取字符串,请参考以下链接

AndroidJson

解析 Json

我想它可能对你有帮助..

于 2012-06-09T04:44:00.297 回答
0

你 json 字符串Result is not valid看这里有效与否

它应该是

[ { "id": "2", "fullname": "Course 1" }, { "id": "3", "fullname": "Course 2" } ]

JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {

   JSONObject objJson = jsonArray.getJSONObject(i);
   int id = objJson.getInt("id");
   String fullName = objJson.getString("fullname");

}
于 2012-06-09T04:40:58.797 回答