W/System.err:org.json.JSONException:java.lang.String类型的值数组无法转换为JSONObject
我被这个错误搞晕了。我正在检索一个JSON对象以显示在Android Studio中的recyclerview中。我在前面的答案的基础上做了一些修改,但似乎没有什么不同 代码接收一个带有嵌套JSON数组(“数据”)的JSON对象,其中包含一个收据列表 示例JSONW/System.err:org.json.JSONException:java.lang.String类型的值数组无法转换为JSONObject,java,android,json,Java,Android,Json,我被这个错误搞晕了。我正在检索一个JSON对象以显示在Android Studio中的recyclerview中。我在前面的答案的基础上做了一些修改,但似乎没有什么不同 代码接收一个带有嵌套JSON数组(“数据”)的JSON对象,其中包含一个收据列表 示例JSON { "status": true, "message": "Data fetched successfully", "data": [ { "id": "2", "receipt_id":
{
"status": true,
"message": "Data fetched successfully",
"data": [
{
"id": "2",
"receipt_id": "2",
"parent_id": "0",
"has_children": "0",
"product_id": "4",
"product_code": "24904",
"product_name": "LIGHT SOLAR S/STEEL GLASS LENS COLOUR CHANGE PDQ H25.5CM",
"quantity_expected": "120",
"quantity_received": "120",
"quantity_putaway": "120",
"quantity_held": "0",
"reason_held": "",
"unit_quantity": "0",
"subinner_uom": "",
"subinner_quantity": "0",
"subinner_unit_quantity": "0",
"subinner_weight": "0.000",
"subinner_cubic": "0.000000",
"inner_uom": "",
"inner_quantity": "0",
"inner_unit_quantity": "0",
"inner_weight": "0.000",
"inner_cubic": "0.000000",
"outer_expected": "10",
"outer_uom": "CTN",
"outer_quantity": "10",
"outer_unit_quantity": "12",
"outer_weight": "0.000",
"outer_cubic": "0.000000",
"outer_length": "0.000",
"outer_width": "0.000",
"outer_height": "0.000",
"pallet_uom": "",
"pallet_quantity": "0",
"pallet_unit_quantity": "0",
"pallet_weight": "0.000",
"pallet_cubic": "0.000000",
"batch_number": " ",
"date_expiry": "0000-00-00",
"serial_number": "",
"length": "36.000",
"width": "29.000",
"height": "29.000",
"cubic": "0.030276",
"weight": "3.000",
"barcode": "",
"cost_current": "0.00",
"status": "0",
"is_uniform_pallet": "0",
"pallet_id": "",
"is_checked": "1",
"is_putaway": "1",
"location_id": "1757",
"last_location": "10139",
"split_key": "V09AFONYL7",
"created_by": "9",
"modified_by": "6",
"datetime_created": "2019-08-13 12:11:06",
"datetime_modified": "2019-08-14 16:58:52"
},
{
"id": "3",
"receipt_id": "2",
"parent_id": "0",
"has_children": "0",
"product_id": "3",
"product_code": "24900",
"product_name": "LIGHT SOLAR FAIRY WH LED PK100",
"quantity_expected": "1008",
"quantity_received": "1008",
"quantity_putaway": "1008",
"quantity_held": "0",
"reason_held": "",
"unit_quantity": "0",
"subinner_uom": "",
"subinner_quantity": "0",
"subinner_unit_quantity": "0",
"subinner_weight": "0.000",
"subinner_cubic": "0.000000",
"inner_uom": "",
"inner_quantity": "0",
"inner_unit_quantity": "0",
"inner_weight": "0.000",
"inner_cubic": "0.000000",
"outer_expected": "84",
"outer_uom": "CTN",
"outer_quantity": "84",
"outer_unit_quantity": "12",
"outer_weight": "0.000",
"outer_cubic": "0.000000",
"outer_length": "0.000",
"outer_width": "0.000",
"outer_height": "0.000",
"pallet_uom": "",
"pallet_quantity": "0",
"pallet_unit_quantity": "0",
"pallet_weight": "0.000",
"pallet_cubic": "0.000000",
"batch_number": " ",
"date_expiry": "0000-00-00",
"serial_number": "",
"length": "36.000",
"width": "23.000",
"height": "23.000",
"cubic": "0.019044",
"weight": "3.500",
"barcode": "",
"cost_current": "0.00",
"status": "0",
"is_uniform_pallet": "0",
"pallet_id": "",
"is_checked": "1",
"is_putaway": "1",
"location_id": "1512",
"last_location": "0",
"split_key": "",
"created_by": "9",
"modified_by": "6",
"datetime_created": "2019-08-13 12:11:06",
"datetime_modified": "2019-08-14 16:58:52"
},
获取JSON
private void fetchJSON(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ReceiptsLineInterface.JSONURL)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
ReceiptsLineInterface api = retrofit.create(ReceiptsLineInterface.class);
Call<String> call = api.getString();
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
Log.i("Responsestring", response.body());
//Toast.makeText()
if (response.isSuccessful()) {
if (response.body() != null) {
Log.i("onSuccess", response.body());
setRecyclerItems(response.body());
} else {
//Log.i("onEmptyResponse", "Returned empty response");//Toast.makeText(getContext(),"Nothing returned",Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
}
private void fetchJSON(){
改装改装=新改装.Builder()
.baseUrl(ReceiptsLineInterface.JSONURL)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
ReceiptsLineInterface api=改造.create(ReceiptsLineInterface.class);
Call Call=api.getString();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
Log.i(“Responsestring”,response.body());
//Toast.makeText()
if(response.issusccessful()){
if(response.body()!=null){
Log.i(“onSuccess”,response.body());
setRecyclerItems(response.body());
}否则{
//Log.i(“onEmptyResponse”,“返回空响应”);//Toast.makeText(getContext(),“Nothing Returned”,Toast.LENGTH_LONG).show();
}
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
}
带错误的代码块
/**
* Array list generator that should take JSON array from API query of Job
* or Item list
* @return items
*/
public void setRecyclerItems(String response){
try {
//getting the whole json object from the response
JSONObject obj = new JSONObject(response);
if(obj.optString("status").equals("true")){
ArrayList<Receipts> modelRecyclerArrayList = new ArrayList<>();
JSONArray dataArray = obj.getJSONArray("data");
for (int i = 0; i < dataArray.length(); i++) {
Receipts modelRecycler = new Receipts();
JSONObject dataobj = dataArray.getJSONObject(i);
modelRecycler.setName(dataobj.getString("product_name"));
modelRecycler.setProductCode(dataobj.getString("product_code"));
modelRecycler.setReceiptID(dataobj.getString("receipt_id"));
modelRecyclerArrayList.add(modelRecycler);
}
retrofitAdapter = new RetrofitAdapter(this,modelRecyclerArrayList);
recyclerView.setAdapter(retrofitAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
}else {
Toast.makeText(getApplicationContext(), obj.optString("message")+"", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
*数组列表生成器,应从作业的API查询中获取JSON数组
*或项目列表
*@归还物品
*/
公共void setRecyclerItems(字符串响应){
试一试{
//从响应中获取整个json对象
JSONObject obj=新的JSONObject(响应);
如果(对象选项字符串(“状态”)等于(“真”)){
ArrayList modelRecyclerArrayList=新建ArrayList();
JSONArray dataArray=obj.getJSONArray(“数据”);
对于(int i=0;i
Logcat响应
2019-12-16 14:39:33.498 8562-8562/com.prostock.scanner I/onSuccess: array (
'status' => true,
'message' => 'Data fetched successfully',
'data' =>
array (
0 =>
array (
'id' => '1',
'receipt_id' => '1',
'parent_id' => '0',
'has_children' => '0',
'product_id' => '26',
'product_code' => '57280',
'product_name' => 'LIGHTS FAIRY CONNECTABLE 400',
'quantity_expected' => '400',
'quantity_received' => '400',
'quantity_putaway' => '400',
'quantity_held' => '0',
'reason_held' => '',
'unit_quantity' => '0',
'subinner_uom' => '',
'subinner_quantity' => '0',
'subinner_unit_quantity' => '0',
'subinner_weight' => '0.000',
'subinner_cubic' => '0.000000',
'inner_uom' => '',
'inner_quantity' => '0',
'inner_unit_quantity' => '0',
'inner_weight' => '0.000',
'inner_cubic' => '0.000000',
'outer_expected' => '50',
'outer_uom' => 'CTN',
'outer_quantity' => '50',
'outer_unit_quantity' => '8',
'outer_weight' => '0.000',
'outer_cubic' => '0.000000',
'outer_length' => '0.000',
'outer_width' => '0.000',
'outer_height' => '0.000',
'pallet_uom' => '',
'pallet_quantity' => '0',
'pallet_unit_quantity' => '0',
'pallet_weight' => '0.000',
'pallet_cubic' => '0.000000',
'batch_number' => ' ',
'date_expiry' => '0000-00-00',
'serial_number' => '',
'length' => '50.000',
'width' => '22.000',
'height' => '36.000',
'cubic' => '0.039600',
'weight' => '8.700',
'barcode' => '',
'cost_current' => '0.00',
'status' => '0',
'is_uniform_pallet' => '1',
'pallet_id' => '',
'is_checked' => '1',
'is_putaway' => '1',
'location_id' => '1641',
'last_location' => '1562',
'split_key' => '',
'created_by' => '9',
'modified_by' => '9',
'datetime_created' => '2019-08-06 12:18:23',
'datetime_modified' => '2019-08-06 16:30:34',
),
),
)
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err: org.json.JSONException: Value array of type java.lang.String cannot be converted to JSONObject
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err: at org.json.JSON.typeMismatch(JSON.java:112)
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err: at org.json.JSONObject.<init>(JSONObject.java:163)
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err: at org.json.JSONObject.<init>(JSONObject.java:176)
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err: at com.prostock.scanner.Activities.ScanSessionActivity.setRecyclerItems(ScanSessionActivity.java:154)
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err: at com.prostock.scanner.Activities.ScanSessionActivity$1.onResponse(ScanSessionActivity.java:130)
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err: at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$1.run(DefaultCallAdapterFactory.java:83)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err: at android.os.Handler.handleCallback(Handler.java:873)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err: at android.os.Looper.loop(Looper.java:193)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6669)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-12-16 14:39:33.498 8562-8562/com.prostock.scanner I/on成功:阵列(
“状态”=>true,
'消息'=>'已成功获取数据',
“数据”=>
排列(
0 =>
排列(
'id'=>'1',
“收据id”=>“1”,
'parent_id'=>'0',
'has_children'=>'0',
“产品标识”=>“26”,
“产品代码”=>“57280”,
“产品名称”=>“可连接400”,
“预计数量”=>“400”,
“收到的数量”=>“400”,
“数量”=>“400”,
“持有数量”=>“0”,
“原因”=>“,
“单位数量”=>“0”,
'子内部单位'=>'',
“子内部数量”=>“0”,
“子内部单位数量”=>“0”,
“子内部重量”=>“0.000”,
“subinner_cubic”=>“0.000000”,
'内部计量单位'=>'',
“内部数量”=>“0”,
“内部单位数量”=>“0”,
“内部重量”=>“0.000”,
“内立方”=>“0.000000”,
“预期为外部”=>“50”,
“外部计量单位”=>“CTN”,
“外部数量”=>“50”,
“外部单位数量”=>“8”,
“外部重量”=>“0.000”,
“外立方”=>“0.000000”,
“外部长度”=>“0.000”,
“外部宽度”=>“0.000”,
“外部高度”=>“0.000”,
“托盘计量单位”=>“”,
“托盘数量”=>“0”,
“托盘单位数量”=>“0”,
“托盘重量”=>“0.000”,
“托盘_cubic”=>“0.000000”,
“批次号”=>“”,
“到期日期”=>“0000-00-00”,
'序列号'=>'',
“长度”=>“50.000”,
“宽度”=>“22.000”,
“高度”=>“36.000”,
“立方”=>“0.039600”,
“重量”=>“8.700”,
“条形码”=>“”,
“当前成本”=>“0.00”,
“状态”=>“0”,
“是统一托盘”=>“1”,
“托盘标识”=>“”,
'是否选中'=>'1',
'is_putaway'=>'1',
'location_id'=>'1641',
“最后位置”=>“1562”,
“拆分键”=>“”,
'由'=>'9'创建'u',
'由'=>'9'修改'u',
“datetime_created”=>“2019-08-06 12:18:23”,
“datetime_modified”=>“2019-08-06 16:30:34”,
),
),
)
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err:org.json.JSONException:无法将java.lang.String类型的值数组转换为JSONObject
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err:at org.json.json.typemissmatch(json.java:112)
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err:at org.json.JSONObject.(JSONObject.java:163)
2019-12-16 14:39:33.499 8562
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ReceiptsLineInterface.JSONURL)
.addConverterFactory(GsonConverterFactory.create()) // this line changed
.build();
import retrofit2.Call;
import retrofit2.http.GET;
public interface ReceiptsLineInterface {
String JSONURL = "http://[censored]/api/putaway/id/{receipts_line}/";
@GET("WRONG ENDPOINT")
Call<String> getString();
}