【问题标题】:Value Next of type java.lang.String cannot be converted to JSONArrayjava.lang.String 类型的值 Next 无法转换为 JSONArray
【发布时间】:2018-04-22 03:16:16
【问题描述】:

我正在尝试将 json_data 从服务器保存到 sqlite,但出现错误

Value Next of type java.lang.String cannot be converted to JSONArray

这是我的 json:My Json

这是我的java代码:

String json_data = stringBuilder.toString().trim();
JSONObject object = new JSONObject(json_data);
JSONObject object1 = new JSONObject("Next");
JSONArray Jarray  = object1.getJSONArray("Days");
for (int i = 0; i < Jarray.length(); i++)
{
    JSONObject Jasonobject = Jarray.getJSONObject(i);
    dbHelper.putStationsInformation((Jasonobject),sqLiteDatabase);
}
dbHelper.close();

我知道这段代码看起来很奇怪,但请帮我这样做。

【问题讨论】:

  • JSONObject object1 = new JSONObject("Next"); - 真的吗?
  • 有时我不明白人们是如何从服务器获得响应的,但不明白对象映射结构如何工作或如何使用该死的调试器。跨度>

标签: java android json sqlite


【解决方案1】:

您需要获取Nextjsonobject,而不是在这里使用单个String 来创建它

new JSONObject("Next"); ,所以从响应中获取它object like

JSONObject object1 = object.getJSONObject("Next");

【讨论】:

    【解决方案2】:

    "Java.lang.String 类型的值 Next 无法转换为 JSON数组”。

    纠正getJSONObject部分。

    不要

     JSONObject object1 = new JSONObject("Next");
    

     JSONObject obj = new JSONObject(json_data );
     JSONObject _jsonOBJ = obj.getJSONObject("Next");
     JSONArray Jarray  = _jsonOBJ .getJSONArray("Days");
     for (int i = 0; i < Jarray.length(); i++)
     {
         JSONObject Jasonobject = Jarray.getJSONObject(i);
         dbHelper.putStationsInformation((Jasonobject),sqLiteDatabase);
     }
    

    【讨论】:

      【解决方案3】:
      JSONObject result_obj= new JSONObject(result.toString());
       JSONObject result_array= result_obj.getJSONObject("Next");
       JSONArray array_values = result_array.getJSONArray("Days");
       for (int i = 0; i < Jarray.length(); i++)
       {
           JSONObject plan = Jarray.getJSONObject(i);
            plan.getString("Day");
            for(int j=0;j<plan.lenght();j++){
      
      
             }
      
       }
      

      【讨论】:

        【解决方案4】:

        您需要从提供的 json 中解析“Next”对象。您创建了一个错误的新 JSONObject。

          try {
                String json_data = stringBuilder.toString().trim();
                JSONObject object = new JSONObject(json_data);
                JSONObject object1 =object.getJSONObject("Next");
                JSONArray Jarray  = object1.getJSONArray("Days");
                for (int i = 0; i < Jarray.length(); i++)
                {
                    JSONObject Jasonobject = Jarray.getJSONObject(i);
                    dbHelper.putStationsInformation((Jasonobject),sqLiteDatabase);
                    dbHelper.close();
        
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        

        编码愉快!!

        【讨论】:

          【解决方案5】:

          您已经创建了一个新的 JSONObject("Next"),这意味着您正在将字符串“Next”转换为 JSONObject。 纠正这个:

          String json_data = stringBuilder.toString().trim();
          JSONObject object = new JSONObject(json_data);
          JSONObject object1 = object.getJSONObject("Next"); 
          

          【讨论】:

            猜你喜欢
            • 2016-02-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-03-02
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多