Java 改装JSON解析时出现错误的IllegalStateException
请帮我解析JSON。我总是得到一个对象: IllegalStateException:应为BEGIN\u数组,但在处为BEGIN\u对象 第1行第2列路径$ 我尝试分析Java 改装JSON解析时出现错误的IllegalStateException,java,android,json,retrofit2,Java,Android,Json,Retrofit2,请帮我解析JSON。我总是得到一个对象: IllegalStateException:应为BEGIN\u数组,但在处为BEGIN\u对象 第1行第2列路径$ 我尝试分析列表、和,但得到了相同的验证 这是我的JSON: {"barcodes":[["1212"],["22222222222"],["22222321321"],["23565233665558488"],["2999300031242"],["6"]]} 接口: import java.util.List; import retr
列表
、
和
,但得到了相同的验证
这是我的JSON:
{"barcodes":[["1212"],["22222222222"],["22222321321"],["23565233665558488"],["2999300031242"],["6"]]}
接口:
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
public interface RequestInterface {
@GET("barcodeinfo?getBarcodes")
Call<List<List<String>>> getBarcodeList();
}
主要内容:
void getarray(){
改装改装=新改装.Builder()
.baseUrl(基本URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface服务=reformation.create(RequestInterface.class);
Call Call=service.getBarcodeList();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
试一试{
List BarcodeData=response.body();
Log.d(“MyLog”,BarcodeData.size()+”);
}捕获(例外e){
Log.d(“MyLog”,“存在错误”);
e、 printStackTrace();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.d(“MyLog”,“error”+t.toString());
}
});
}
使用这些POJO类
公共类结果{
@序列化名称(“条形码”)
@暴露
私有列表条形码=新的ArrayList();
/**
*
*@返回
*条形码
*/
公共列表获取条形码(){
返回条码;
}
/**
*
*@param条码
*条形码
*/
公共条形码(列出条形码){
这是。条形码=条形码;
}
}
使用这样的界面
@GET(“条形码信息?获取条形码”)
调用getBarcodeList();
像这样打电话
Call Call=service.getBarcodeList();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
Result r=response.body();//如果在外部使用此响应,则可以初始化Result r变量global
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.d(“MyLog”,“error”+t.toString());
}
});
注意:-您正试图以列表的形式访问,但作为响应,它将出现一个简单的JSON对象
使用这些POJO类
公共类结果{
@序列化名称(“条形码”)
@暴露
私有列表条形码=新的ArrayList();
/**
*
*@返回
*条形码
*/
公共列表获取条形码(){
返回条码;
}
/**
*
*@param条码
*条形码
*/
公共条形码(列出条形码){
这是。条形码=条形码;
}
}
使用这样的界面
@GET(“条形码信息?获取条形码”)
调用getBarcodeList();
像这样打电话
Call Call=service.getBarcodeList();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
Result r=response.body();//如果在外部使用此响应,则可以初始化Result r变量global
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.d(“MyLog”,“error”+t.toString());
}
});
注意:-您正试图以列表的形式访问,但作为响应,它将是一个简单的JSON对象您的JSON不是一个数组。。。那么你期待什么呢?为什么不呢?[1,2,3,4]不是数组吗?JSON数组应该像这样[{1},{2},{3}]?所以我有对象字符串=[1,2,3,4]?{…}不是数组你的json不是数组。。。那么你期待什么呢?为什么不呢?[1,2,3,4]不是数组吗?JSON数组应该像这样[{1},{2},{3}]?所以我有object string=[1,2,3,4]?{…}不是数组谢谢你,它真的很有帮助。谢谢你,它真的很有帮助。
public class SingleBarcode {
final String barcodes;
public SingleBarcode(String barcodes) {
this.barcodes = barcodes;
}
}
void getRetrofitArray() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface service = retrofit.create(RequestInterface.class);
Call<List<List<String>>> call = service.getBarcodeList();
call.enqueue(new Callback<List<List<String>>>() {
@Override
public void onResponse(Call<List<List<String>>> call, Response<List<List<String>>> response) {
try {
List<List<String>> BarcodeData = response.body();
Log.d("MyLog", BarcodeData.size()+"");
} catch (Exception e) {
Log.d("MyLog", "There is an error");
e.printStackTrace();
}
}
@Override
public void onFailure(Call<List<List<String>>> call, Throwable t) {
Log.d("MyLog", "error " + t.toString());
}
});
}
public class Result {
@SerializedName("barcodes")
@Expose
private List<List<String>> barcodes = new ArrayList<List<String>>();
/**
*
* @return
* The barcodes
*/
public List<List<String>> getBarcodes() {
return barcodes;
}
/**
*
* @param barcodes
* The barcodes
*/
public void setBarcodes(List<List<String>> barcodes) {
this.barcodes = barcodes;
}
}
@GET("barcodeinfo?getBarcodes")
Call<Result> getBarcodeList();
Call<Result> call = service.getBarcodeList();
call.enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
Result r = response.body(); // you can initialize result r variable global if you have out side use of this response
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
Log.d("MyLog", "error " + t.toString());
}
});