【问题标题】:JsonException Value stdClass of type java.lang.String cannot be converted to JSONObject?java.lang.String 类型的 JsonException Value stdClass 无法转换为 JSONObject?
【发布时间】:2015-12-01 12:12:13
【问题描述】:

在这里,我尝试将json 数据发送到我的网络服务器并使用php 我想解析 json 并将数据插入 mysql 数据库。我收到一条错误消息,提示 Value stdClass of type java.lang.String cannot be converted to 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

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

【问题讨论】:

  • 你能显示完整的堆栈跟踪吗?
  • @Patrick 我刚刚添加了堆栈跟踪,请检查。谢谢 :)
  • @Patrick 谢谢 :) 你能帮忙解答吗?
  • 我在任何地方都找不到Value stdClass。你能说出异常真正发生的地方吗?谢谢
  • 检查您的 json 字符串中是否有一些看不见的字符。看看this answer

标签: java android json


【解决方案1】:

在您的代码中,我认为您正在尝试将字符串 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 + "\"");
}

【讨论】:

  • 我的 json 数组类似于 [{"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"}]
  • jsonviewer.stack.hu 打开此链接并通过您的 json 数组。当我通过此 json 数组时,显示无效的 json 数组错误。
  • 您在 JsonObjectRequest 类中有问题,因为上面的代码工作正常。所以请提供这个类的代码。
【解决方案2】:

size 是 1,这是正确的,因为您是这样放入地图的:

params.put("Array",jsonarray);

所以 json 将是 1 对象 {"Array":[...]} 并且您必须先访问该对象才能解析数组。

【讨论】:

  • 如果您检查图像,您会看到 jsonarray 被放入 params(Map) 那么为什么 new JsonObject(params) 会导致 objectRequest:"[] 即为什么它是空白的。我想我我得到了异常,因为它是空白的。你能给出一个关于如何使用 volley 将多个值插入到 mysql 数据库中的代码示例。这对像我这样的初学者非常有帮助 谢谢:)
【解决方案3】:

如果你关注的是我的exmaple,那么请注意我使用了一个

Map<String,Object> 

参数是这样插入的:

Map<String, Object> params = new ArrayMap<>();
// Adding parameters to request
params.put("email", email); // here email is already a string,
params.put("password", password.toString()); // here password needs the conversion 

然后我使用新的匿名 JSONObject 将其直接传递到请求中:

// Creating a JSON Object request
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(params), ...

这样,转换通过 Object 类。

另请注意,我的语法会创建这些参数并将其放在请求之外和之前。

编辑: 之所以看到size是1,是因为其实是1,这部分:

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

使地图大小为 1,因为您只在其中放置了一个对象 - key-value ma​​ppings 的数量是为映射大小给出的值,意思是 - 指向一个值的键的数量(并且值本身可以是另一个 Map,它仍然只能算作一),请参阅This Documentation以获取有关地图集的完整说明。

关于另一部分 - 我建议尝试将您想要的值添加到 Map 并在新的 JSONObject 中发送 that,我回答了一些可能对您有帮助的 Here。 然后 - 在 php 方面 - 打印出你得到的东西(看看你确实得到了它),一旦你在 php 方面得到了参数 - 你就可以开始用你的头来解析它了。

希望这对您有任何帮助:)

【讨论】:

  • 请检查更新后的问题。我的问题是当我调试新的 JSONObject(params) 时显示 params:size=1 和一个空的 objectRequest[]。
猜你喜欢
  • 1970-01-01
  • 2016-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多