Android Can';我无法理解这个JSON解析错误
目前,我正在尝试在Android的Android Can';我无法理解这个JSON解析错误,android,json,Android,Json,目前,我正在尝试在Android的列表视图中显示JSON数据(托管在Web服务器上)。应用程序正确地接收到数据,但无法进一步处理以在所述列表视图中显示数据 错误如下: JSON解析错误:值。。。无法将org.json.JSONArray类型转换为JSONObject 我试图解析的JSON数据如下所示: 处理接收到的JSON字符串的代码: try{ JSONObject jsonObject = new JSONObject(jsonStr); JSONArray books = jso
列表视图中显示JSON数据(托管在Web服务器上)。应用程序正确地接收到数据,但无法进一步处理以在所述列表视图中显示数据
错误如下:
JSON解析错误:值。。。无法将org.json.JSONArray类型转换为JSONObject
我试图解析的JSON数据如下所示:
处理接收到的JSON字符串的代码:
try{
JSONObject jsonObject = new JSONObject(jsonStr);
JSONArray books = jsonObject.getJSONArray("book");
for(int i = 0; i < books.length(); i++){
JSONObject obj = books.getJSONObject(i);
String idBook = obj.getString("idBuch");
String author = obj.getString("autor");
String name = obj.getString("name");
String price = obj.getString("preis");
JSONObject booktype = obj.getJSONObject("buchtyp");
String idBooktype = booktype.getString("idBuchtyp");
String typename = booktype.getString("typenamen");
HashMap<String, String> book = new HashMap<>();
book.put("idBook", idBook);
book.put("author", author);
book.put("name", name);
book.put("price", price);
book.put("genre", typename);
bookList.add(book);
} }catch(final JSONException e)
试试看{
JSONObject JSONObject=新的JSONObject(jsonStr);
JSONArray books=jsonObject.getJSONArray(“book”);
for(int i=0;i
我知道这个网站上有很多类似的问题,但我在这个问题上仍然没有成功。提前感谢。您提供的JSON只包含一个数组
[
{
"idBuch": 1,
"autor": "Erich Maria Remarque",
"name": "Im Westen nichts Neues",
"preis": 20,
"buchtyp": {
"idBuchtyp": 3,
"typenamen": "Geschichte"
}
}
]
但是,您的代码希望根目录是具有外业手簿的对象
{
"book": [
{
"idBuch": 1,
"autor": "Erich Maria Remarque",
"name": "Im Westen nichts Neues",
"preis": 20,
"buchtyp": {
"idBuchtyp": 3,
"typenamen": "Geschichte"
}
}
]
}
在这种情况下,请尝试更换该行:
JSONObject jsonObject = new JSONObject(jsonStr);
与
一切照常进行。您的最终结果应该如下所示:
try {
JSONArray books = new JSONArray(jsonStr);
for (int i = 0; i < books.length(); i++) {
JSONObject obj = books.getJSONObject(i);
String idBook = obj.getString("idBuch");
String author = obj.getString("autor");
String name = obj.getString("name");
String price = obj.getString("preis");
JSONObject booktype = obj.getJSONObject("buchtyp");
String idBooktype = booktype.getString("idBuchtyp");
String typename = booktype.getString("typenamen");
HashMap < String, String > book = new HashMap < > ();
book.put("idBook", idBook);
book.put("author", author);
book.put("name", name);
book.put("price", price);
book.put("genre", typename);
bookList.add(book);
}
} catch (final JSONException e) {
e.printStackTrace()
}
试试看{
JSONArray books=新的JSONArray(jsonStr);
for(int i=0;ibook=newhashmap<>();
book.put(“idBook”,idBook);
书。放(“作者”,作者);
书。写(“名字”,名字);
账面卖出价(“价格”,价格);
书。字(“体裁”,字体名称);
图书目录。添加(图书);
}
}捕获(最终JSONException e){
e、 printStackTrace()
}
您提供的JSON只包含一个数组
[
{
"idBuch": 1,
"autor": "Erich Maria Remarque",
"name": "Im Westen nichts Neues",
"preis": 20,
"buchtyp": {
"idBuchtyp": 3,
"typenamen": "Geschichte"
}
}
]
但是,您的代码希望根目录是具有外业手簿的对象
{
"book": [
{
"idBuch": 1,
"autor": "Erich Maria Remarque",
"name": "Im Westen nichts Neues",
"preis": 20,
"buchtyp": {
"idBuchtyp": 3,
"typenamen": "Geschichte"
}
}
]
}
在这种情况下,请尝试更换该行:
JSONObject jsonObject = new JSONObject(jsonStr);
与
一切照常进行。您的最终结果应该如下所示:
try {
JSONArray books = new JSONArray(jsonStr);
for (int i = 0; i < books.length(); i++) {
JSONObject obj = books.getJSONObject(i);
String idBook = obj.getString("idBuch");
String author = obj.getString("autor");
String name = obj.getString("name");
String price = obj.getString("preis");
JSONObject booktype = obj.getJSONObject("buchtyp");
String idBooktype = booktype.getString("idBuchtyp");
String typename = booktype.getString("typenamen");
HashMap < String, String > book = new HashMap < > ();
book.put("idBook", idBook);
book.put("author", author);
book.put("name", name);
book.put("price", price);
book.put("genre", typename);
bookList.add(book);
}
} catch (final JSONException e) {
e.printStackTrace()
}
试试看{
JSONArray books=新的JSONArray(jsonStr);
for(int i=0;ibook=newhashmap<>();
book.put(“idBook”,idBook);
书。放(“作者”,作者);
书。写(“名字”,名字);
账面卖出价(“价格”,价格);
书。字(“体裁”,字体名称);
图书目录。添加(图书);
}
}捕获(最终JSONException e){
e、 printStackTrace()
}
您的代码与测试字符串不兼容;代码正在查找{“book”:[…]}
如果您的JSON与编写的一样,那么您应该从执行JSONArray books=new JSONArray(jsonStr)
开始,而不是前两行。我的错误…但是删除它或再次使用jsonStr
没有任何区别。您的代码与测试字符串不兼容;代码正在查找{“book”:[…]}
如果您的JSON与编写的一样,那么您应该从JSONArray books=new JSONArray(jsonStr)
开始,而不是前两行。我的错……但是删除它或再次使用jsonStr
没有什么区别。