Pleace,幫我解析JSON。我總是得到一個對象:錯誤的IllegalStateException在Retrofit JSON解析
IllegalStateException異常:預期BEGIN_ARRAY但 行1列2路$
我嘗試收杆List<List<String>>
,<List<String>>
和<String>
但得到相同的exeption是BEGIN_OBJECT。
這裏是我的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();
}
OBJ:
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());
}
});
}
您的JSON是不是數組......有啥你預期的 – Selvin
爲什麼不呢? [1,2,3,4]不是數組? JSON數組應該看起來像這樣[{1},{2},{3}]?所以我有對象字符串= [1,2,3,4]? –
{...}不是數組 – Selvin