java.lang.String 类型的值 Next 无法转换为 JSONArray
Posted
技术标签:
【中文标题】java.lang.String 类型的值 Next 无法转换为 JSONArray【英文标题】:Value Next of type java.lang.String cannot be converted to 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");
- 真的吗?
有时我不明白人们是如何从服务器获得响应的,但不明白对象映射结构如何工作或如何使用该死的调试器。跨度>
【参考方案1】:
您需要获取Next
jsonobject
,而不是在这里使用单个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");
【讨论】:
以上是关于java.lang.String 类型的值 Next 无法转换为 JSONArray的主要内容,如果未能解决你的问题,请参考以下文章
无法将 java.lang.String 类型的值转换为所需的 java.util.Date 类型
JSONException:java.lang.String 类型的值无法转换为 JSONObject
无法将类型 [org.springframework.web.multipart.MultipartFile] 的值转换为所需类型 [java.lang.String]
JSONException:Java.lang.String 类型的值 <?xml 无法转换为 JSONObject
“JSONException:Java.lang.String 类型的值 <br 无法转换为 JSONObject”? [复制]
org.json.JSONException:java.lang.String 类型的值连接无法转换为 JSONArray