java - JsonException java.lang.String 类型的值 stdClass 无法转换为 JSONObject?

标签 java android json

在这里,我尝试将 json 数据发送到我的网络服务器,并使用 php 我想解析 json 并将数据插入到 mysql 数据库中。我收到一条错误消息,指出 type java.lang.String 的 Value stdClass 无法转换为 JSONObject。

如何解决此错误?

   private void insertToDb() throws JSONException {
    billType = (invEstSwitch.isChecked() ? textViewEstimate : textViewInvoice)
            .getText().toString();
    String invNumber = textViewInvNo.getText().toString();
    String bcode = barCode.getText().toString();
    String description = itemDesc.getText().toString();
    String wt = weightLine.getText().toString();
    String rateAmt = rateAmount.getText().toString();
    String making = makingAmount.getText().toString();
    String netr = netRate.getText().toString();
    String iTotal=itemtotal.getText().toString();
    String vatAmt =textViewVat.getText().toString();
    String sumAmt =textViewSum.getText().toString();
    String crtDate =textViewCurrentDate.getText().toString();
    try {
        jsonObject.put("custInfo", custSelected.toString());
        jsonObject.put("invoiceNo", invNumber);
        jsonObject.put("barcode", bcode);
        jsonObject.put("desc", description);
        jsonObject.put("weight",wt );
        jsonObject.put("rate", rateAmt);
        jsonObject.put("makingAmt", making);
        jsonObject.put("net_rate",netr);
        jsonObject.put("itemTotal",iTotal );
        jsonObject.put("vat", vatAmt);
        jsonObject.put("sum_total", sumAmt);
        jsonObject.put("bill_type", billType);
        jsonObject.put("date", crtDate);


    } catch (JSONException e) {
        e.printStackTrace();
    }
    try {
        itemSelectedJson.put(index, jsonObject);
        index++;
    } catch (JSONException e) {
        e.printStackTrace();
    }
   String jsonarray = itemSelectedJson.toString().trim();
    JSONObject jb= new JSONObject();
    jb.put("selected",itemSelectedJson);

 Map<String,Object> params = new HashMap<>();
    params.put("Array",jsonarray);


    final JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.POST, INVEST_URL, new JSONObject(params), new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.d("RESPONSE", response.toString());

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("JSONERROR",error.toString());
        }
    });



    final RequestQueue queue = Volley.newRequestQueue(this);
    queue.add(objectRequest);

}

我是编程新手,因此非常感谢您的帮助。谢谢:)

这是堆栈跟踪:

12-01 17:17:50.840 16785-16785/org.bordetuts.com.goldmine D/dalvikvm: GC_FOR_ALLOC freed 345K, 9% free 8597K/9396K, paused 14ms, total 14ms
12-01 17:17:50.841 16785-16785/org.bordetuts.com.goldmine D/SELECTED_ITEMS: [{"custInfo":"Ujwal  9975022560","rate":"24000","weight":"21.00000","desc":"GENTS ANGTHI 22k NO STONE","makingAmt":"200","sum_total":"RS.52094.46","vat":"RS.1021.46","itemTotal":"51073","barcode":"BQSP78BB","net_rate":"24200","date":"2015-12-01","invoiceNo":"1","bill_type":"Invoice"}]
12-01 17:17:50.897 16785-16785/org.bordetuts.com.goldmine W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
12-01 17:17:50.897 16785-16785/org.bordetuts.com.goldmine W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
12-01 17:17:51.259 16785-16785/org.bordetuts.com.goldmine D/JSONERROR: com.android.volley.ParseError: org.json.JSONException: Value stdClass of type java.lang.String cannot be converted to JSONObject
12-01 17:21:47.657 16785-16792/org.bordetuts.com.goldmine D/dalvikvm: GC_FOR_ALLOC freed 536K, 8% free 8689K/9396K, paused 171ms, total 190ms

enter image description here这是我调试它时看到的。问题是当我说 JSONObject(params) 时它说 params: size=1 objectRequest:"[]

最佳答案

在您的代码中,我认为您正在尝试将字符串 stdClass 转换为 json 对象,因此如果您正在执行此过程,请按照下面的代码操作,我给出了一个将字符串转换为 json 对象的示例

String json = {"phonetype":"N95","cat":"WP"};

try {

    JSONObject obj = new JSONObject(json);

    Log.d("My App", obj.toString());

} catch (Throwable t) {
    Log.e("My App", "Could not parse malformed JSON: \"" + json + "\"");
}

关于java - JsonException java.lang.String 类型的值 stdClass 无法转换为 JSONObject?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34020180/

相关文章:

json - 如何将带有小写成员的简单json解码为结构

java - 如何使用 java 在控制台的 excel 表中使用 poi(2.5) 打印大于 7 位的数字

java - 如何从default-synchronization-context.xml重新定义Spring bean

java正则表达式发现正则表达式

android - DragEventListener 不适用于三星 S7 (6.0)

android - 在xml文件中实例化一个内部类(Preference)

android - ViewPager、兼容包和 Ice Cream Sandwich

jquery - 使用 jQuery 创建和附加数据 block

java - 我如何使用 Jackson 解析以下 JSON。为使用 Jackson ObjectMapper 创建的最相关的 java 对象是什么?

java - 如何打印数组中 2 个参数的相同对象的值总和