【发布时间】:2016-06-17 13:00:06
【问题描述】:
我必须将 ID 和最小数量转换为存储到共享首选项的数组的 JSON 对象。 我可以从 Volley 中的 JSON 对象中获取字符串 ID 和 Min Quantity。这两项都必须存储在对象的 Shared Preference Array 中,正如我在 JSON 下面提到的那样。
Json:(我想这样存储到共享首选项)
{
[
"producutid" : 1
"minqty" : 5
]
,
[
"producutid" : 2
"minqty" : 5
]
,
[
"producutid" : 3
"minqty" : 5
]
,
[
"producutid" : 4
"minqty" : 5
]
}
我的工作代码:
String channel = shared.getString(Constants.cartid, "{'[]','[]'}");
JSONArray items = null;
String wishitem;
JSONObject jo = null;
if (TextUtils.isEmpty(channel)) {
jo = new JSONObject();
try {
jo.getJSONArray(String.valueOf(0)).put(String.valueOf(productpathid));
jo.getJSONArray(String.valueOf(1)).put(String.valueOf(minqty));
} catch (JSONException e) {
e.printStackTrace();
}
wishitem = String.valueOf(items);
editor.putString(Constants.cartid, wishitem);
editor.apply();
} else {
try {
Boolean found = false;
jo = new JSONObject(String.valueOf(channel));
items = jo.getJSONArray(String.valueOf(0));
for (int x = 0; x < items.length(); x++) {
if (productpathid.equalsIgnoreCase(items.getString(x))) {
found = true;
}
}
if (!found) {
try {
jo.getJSONArray(String.valueOf(0)).put(String.valueOf(productpathid));
jo.getJSONArray(String.valueOf(1)).put(String.valueOf(minqty));
} catch (JSONException e) {
e.printStackTrace();
}
wishitem = String.valueOf(items);
editor.putString(Constants.cartid, wishitem);
editor.apply();
}
editor.apply();
} catch (JSONException e) {
e.printStackTrace();
}}
我有错误
org.json.JSONException: Value [] of type org.json.JSONArray cannot be converted to JSONObject
我犯了一些错误,谁能告诉我这段代码的解决方案。 提前致谢。
【问题讨论】:
标签: android json android-layout sharedpreferences