无法在改造 2.0 中使用两种不同的数据类型解析 json
Cannot parse json with two different data types in retrofit 2.0
我正在尝试将下面的 json 解析为一个名为 AlbumResponse
的对象,该对象内部还有另外两个对象,Album
和 PaginationInfo
。改造版本 2.0
[
{
"id": "6",
"name": "King Stays King",
"artist_name":"Timbaland",
"image":""
},
{
"id": "7",
"name": "East Atlanta Santa 2",
"artist_name":"Gucci Mane",
"image":""
},
{
"id": "8",
"name": "The cuban connect",
"artist_name":"Phophit",
"image":""
},
{
"id": "9",
"name": "Shmoney Keeps",
"artist_name":"Calling",
"image":""
},
{
"id": "10",
"name": "Cabin Fever 3",
"artist_name":"Wiz khalifa",
"image":""
}
],
{
"nextPage": "http://private-ede172-mymixtapez1.apiary-mock.com/features/page_3/",
"itemsTotal": 10,
"page": 2,
"pagerMax": 2
}
专辑class
public class Album {
long id;
String name;
@SerializedName("artist_name")
String artistName;
String image;
}
分页信息class
public class PaginationInfo {
int page;
int pagerMax;
int nextPage;
int itemsTotal;
}
AlbumResponse,上面有两个 class,Album
是 List
public class AlbumResponse {
public List<Album> albums;
public PaginationInfo paginationInfo;
}
要求
Call<AlbumResponse> responseCall = albumService.features();
responseCall.enqueue(new Callback<AlbumResponse>() {
@Override
public void onResponse(Response<AlbumResponse> response, Retrofit retrofit) {
if(response.isSuccess()) {
AlbumResponse albumResponse = response.body();
PaginationInfo paginationInfo = albumResponse.getPaginationInfo();
}
System.out.println();
}
@Override
public void onFailure(Throwable t) {
System.out.println(t.getMessage());
}
});
界面
public interface AlbumService {
@GET("/features/")
Call<AlbumResponse> features();
}
问题是我得到一个 Throwable
包含:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $`
拜托,有人可以帮助我吗,我在 Whosebug 中没有找到任何答案。谢谢。
错误说:解析器期望 JSON-Object 但它读取 JSON-array。要修复它(如果您控制服务器),您应该将 JSON 字符串更改为如下内容:
{
"albums" : [
{
"id": "6",
"name": "King Stays King",
"artist_name":"Timbaland",
"image":""
},
{
"id": "7",
"name": "East Atlanta Santa 2",
"artist_name":"Gucci Mane",
"image":""
},
{
"id": "8",
"name": "The cuban connect",
"artist_name":"Phophit",
"image":""
},
{
"id": "9",
"name": "Shmoney Keeps",
"artist_name":"Calling",
"image":""
},
{
"id": "10",
"name": "Cabin Fever 3",
"artist_name":"Wiz khalifa",
"image":""
}
],
"paginationInfo" : {
"nextPage": "http://private-ede172-mymixtapez1.apiary-mock.com/features/page_3/",
"itemsTotal": 10,
"page": 2,
"pagerMax": 2
}
}
现在是 JSON-Object 并且符合您的 Java class。
如果你不能在后端改变JSON,我会把它作为行响应并使用GSON或手动分别解析albums Array
和PaginationInfo
。
顺便说一句。您必须在 PaginationInfo
class
中将 nextPage
类型从 int
更改为 String
您的 JSON 初始声明有问题。
根据json.org:
JSON is built on two structures:
• A collection of name/value pairs. In various languages, this is
realized as an object, record, struct, dictionary, hash table, keyed
list, or associative array.
• An ordered list of values. In most
languages, this is realized as an array, vector, list, or sequence.
尝试将您的 JSON 更新为:
"albums": [
{
"id": "6",
"name": "King Stays King",
"artist_name":"Timbaland",
"image":""
},
{
"id": "7",
"name": "East Atlanta Santa 2",
"artist_name":"Gucci Mane",
"image":""
},
{
"id": "8",
"name": "The cuban connect",
"artist_name":"Phophit",
"image":""
},
{
"id": "9",
"name": "Shmoney Keeps",
"artist_name":"Calling",
"image":""
},
{
"id": "10",
"name": "Cabin Fever 3",
"artist_name":"Wiz khalifa",
"image":""
}
],
"pagination_info":
{
"nextPage": "http://private-ede172-mymixtapez1.apiary-mock.com/features/page_3/",
"itemsTotal": 10,
"page": 2,
"pagerMax": 2
}
我正在尝试将下面的 json 解析为一个名为 AlbumResponse
的对象,该对象内部还有另外两个对象,Album
和 PaginationInfo
。改造版本 2.0
[
{
"id": "6",
"name": "King Stays King",
"artist_name":"Timbaland",
"image":""
},
{
"id": "7",
"name": "East Atlanta Santa 2",
"artist_name":"Gucci Mane",
"image":""
},
{
"id": "8",
"name": "The cuban connect",
"artist_name":"Phophit",
"image":""
},
{
"id": "9",
"name": "Shmoney Keeps",
"artist_name":"Calling",
"image":""
},
{
"id": "10",
"name": "Cabin Fever 3",
"artist_name":"Wiz khalifa",
"image":""
}
],
{
"nextPage": "http://private-ede172-mymixtapez1.apiary-mock.com/features/page_3/",
"itemsTotal": 10,
"page": 2,
"pagerMax": 2
}
专辑class
public class Album {
long id;
String name;
@SerializedName("artist_name")
String artistName;
String image;
}
分页信息class
public class PaginationInfo {
int page;
int pagerMax;
int nextPage;
int itemsTotal;
}
AlbumResponse,上面有两个 class,Album
是 List
public class AlbumResponse {
public List<Album> albums;
public PaginationInfo paginationInfo;
}
要求
Call<AlbumResponse> responseCall = albumService.features();
responseCall.enqueue(new Callback<AlbumResponse>() {
@Override
public void onResponse(Response<AlbumResponse> response, Retrofit retrofit) {
if(response.isSuccess()) {
AlbumResponse albumResponse = response.body();
PaginationInfo paginationInfo = albumResponse.getPaginationInfo();
}
System.out.println();
}
@Override
public void onFailure(Throwable t) {
System.out.println(t.getMessage());
}
});
界面
public interface AlbumService {
@GET("/features/")
Call<AlbumResponse> features();
}
问题是我得到一个 Throwable
包含:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $`
拜托,有人可以帮助我吗,我在 Whosebug 中没有找到任何答案。谢谢。
错误说:解析器期望 JSON-Object 但它读取 JSON-array。要修复它(如果您控制服务器),您应该将 JSON 字符串更改为如下内容:
{
"albums" : [
{
"id": "6",
"name": "King Stays King",
"artist_name":"Timbaland",
"image":""
},
{
"id": "7",
"name": "East Atlanta Santa 2",
"artist_name":"Gucci Mane",
"image":""
},
{
"id": "8",
"name": "The cuban connect",
"artist_name":"Phophit",
"image":""
},
{
"id": "9",
"name": "Shmoney Keeps",
"artist_name":"Calling",
"image":""
},
{
"id": "10",
"name": "Cabin Fever 3",
"artist_name":"Wiz khalifa",
"image":""
}
],
"paginationInfo" : {
"nextPage": "http://private-ede172-mymixtapez1.apiary-mock.com/features/page_3/",
"itemsTotal": 10,
"page": 2,
"pagerMax": 2
}
}
现在是 JSON-Object 并且符合您的 Java class。
如果你不能在后端改变JSON,我会把它作为行响应并使用GSON或手动分别解析albums Array
和PaginationInfo
。
顺便说一句。您必须在 PaginationInfo
class
nextPage
类型从 int
更改为 String
您的 JSON 初始声明有问题。
根据json.org:
JSON is built on two structures:
• A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
• An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
尝试将您的 JSON 更新为:
"albums": [
{
"id": "6",
"name": "King Stays King",
"artist_name":"Timbaland",
"image":""
},
{
"id": "7",
"name": "East Atlanta Santa 2",
"artist_name":"Gucci Mane",
"image":""
},
{
"id": "8",
"name": "The cuban connect",
"artist_name":"Phophit",
"image":""
},
{
"id": "9",
"name": "Shmoney Keeps",
"artist_name":"Calling",
"image":""
},
{
"id": "10",
"name": "Cabin Fever 3",
"artist_name":"Wiz khalifa",
"image":""
}
],
"pagination_info":
{
"nextPage": "http://private-ede172-mymixtapez1.apiary-mock.com/features/page_3/",
"itemsTotal": 10,
"page": 2,
"pagerMax": 2
}