【问题标题】:Android java type org.json.JSONObject cannot be converted to JSONArrayAndroid java type org.json.JSONObject 无法转换为 JSONArray
【发布时间】:2015-08-11 14:42:27
【问题描述】:

我正在尝试连接到 Api 链接,读取并构造对象列表中的所有值。

我从我的 活动 中调用 AsyncTask。我检查了列表值,它们都是正确的。

watchlistByDB_AsyncTask watchlistByDB_AsyncTask = new watchlistByDB_AsyncTask(list, this);
watchlistByDB_AsyncTask.setItemListToListings(this);
watchlistByDB_AsyncTask.execute();

watchlistByDB_AsyncTask:

public class watchlistByDB_AsyncTask extends AsyncTask<Void, watchlist_root, Void> {

    JSONObject Jo_result;
    itemListToListings itemListToListings;
    Context context;
    List<DB_constr> items;

    public watchlistByDB_AsyncTask(List<DB_constr> items, Context context) {
        this.items = items;
        this.context = context;
    }



    public void setItemListToListings (itemListToListings itemListToListings) {
        this.itemListToListings = itemListToListings;
    }

    @Override
    protected Void doInBackground(Void... params) {
        for(int i = 0; i < items.size(); i++){
            String url = "http://www.gw2spidy.com/api/v0.9/json/item/" + items.get(i).getItemID();
            publishProgress(JoToJO_constructor(spidyHttpGetRequest(url)));
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(watchlist_root... iacs) {
        super.onProgressUpdate(iacs);
        if (iacs[0] != null) {
            itemListToListings.itemListToListings(iacs[0]);
        } else {
            Log.i("gw2Log", "null");
        }
    }

    public JSONObject spidyHttpGetRequest(String URL){
        try {
            HttpGet get = new HttpGet(URL);
            HttpClient client = new DefaultHttpClient();
            HttpResponse response = client.execute(get);
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity);
            Jo_result = new JSONObject(result);

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return Jo_result;
    }



    public watchlist_root JoToJO_constructor(JSONObject Jo_result) {
        watchlist_root spidy_iTN_rootO = new watchlist_root();
        try {

            JSONArray list = new JSONArray(Jo_result.getString("result"));

            for (int i = 0; i < list.length(); i++) {
                JSONObject resultsObject = list.getJSONObject(i);
                watchlist_result spidy_iTN_resultsO = new watchlist_result();

                spidy_iTN_resultsO.setData_id(resultsObject
                        .getString("data_id"));
                spidy_iTN_resultsO.setName(resultsObject
                        .getString("name"));
                spidy_iTN_resultsO.setRarity(resultsObject
                        .getInt("rarity"));
                spidy_iTN_resultsO.setRestriction_level(resultsObject
                        .getInt("restriction_level"));
                spidy_iTN_resultsO.setImg(resultsObject
                        .getString("img"));
                spidy_iTN_resultsO.setType_id(resultsObject
                        .getInt("type_id"));
                spidy_iTN_resultsO.setSub_type_id(resultsObject
                        .getInt("sub_type_id"));
                spidy_iTN_resultsO.setPrice_last_changed(resultsObject
                        .getString("price_last_changed"));
                spidy_iTN_resultsO.setMax_offer_unit_price(resultsObject
                        .getString("max_offer_unit_price"));
                spidy_iTN_resultsO.setMin_sale_unit_price(resultsObject
                        .getString("min_sale_unit_price"));
                spidy_iTN_resultsO.setOffer_availability(resultsObject
                        .getInt("offer_availability"));
                spidy_iTN_resultsO.setSale_availability(resultsObject
                        .getInt("sale_availability"));
                spidy_iTN_resultsO.setSale_price_change_last_hour(resultsObject
                        .getInt("sale_price_change_last_hour"));
                spidy_iTN_resultsO.setOffer_price_change_last_hour(resultsObject
                        .getInt("offer_price_change_last_hour"));
                spidy_iTN_rootO.addObject(spidy_iTN_resultsO);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return spidy_iTN_rootO;
    }

    public interface itemListToListings {
        public void itemListToListings(watchlist_root resultClass);
    }
}

在尝试运行此代码时出现错误

line 88: JSONArray list = new JSONArray(Jo_result.getString("result"));

org.json.JSONException: Value {"data_id":9586,"name":"18 Slot Mithril Box","rarity":1,"restriction_level":0,"img":"https:\/\/render.guildwars2.com\/file\/00A876DFF0B89D18026B9BE4C1239E0BB7BAB81E\/220595.png","type_id":2,"sub_type_id":0,"price_last_changed":"2015-05-28 20:52:14 UTC","max_offer_unit_price":20800,"min_sale_unit_price":26300,"offer_availability":1106,"sale_availability":465,"sale_price_change_last_hour":0,"offer_price_change_last_hour":0,"result_of":[{"recipe_id":511,"name":"18 Slot Mithril Box"}]} of type org.json.JSONObject cannot be converted to JSONArray
05-28 21:28:35.596  25726-25742/? W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:111)
05-28 21:28:35.596  25726-25742/? W/System.err﹕ at org.json.JSONArray.<init>(JSONArray.java:96)
05-28 21:28:35.596  25726-25742/? W/System.err﹕ at org.json.JSONArray.<init>(JSONArray.java:108)
05-28 21:28:35.596  25726-25742/? W/System.err﹕ at com.example.krijn.gw2TP_androidMobile.AsyncTasks.watchlistByDB_AsyncTask.JoToJO_constructor(watchlistByDB_AsyncTask.java:88)
05-28 21:28:35.596  25726-25742/? W/System.err﹕ at com.example.krijn.gw2TP_androidMobile.AsyncTasks.watchlistByDB_AsyncTask.doInBackground(watchlistByDB_AsyncTask.java:47)
05-28 21:28:35.596  25726-25742/? W/System.err﹕ at com.example.krijn.gw2TP_androidMobile.AsyncTasks.watchlistByDB_AsyncTask.doInBackground(watchlistByDB_AsyncTask.java:25)

错误本身可以自我解释,但我不知道为什么会调用它。 This is one of the links 表明它是一个 JSON 数组。我尝试将每个属性添加为 JSONObject 作为错误状态,但随后它不知道任何结构属性。

【问题讨论】:

  • shows that it is an JSON array 不,它没有。唯一的数组在result_of,而不是result
  • 您查询单个项目(如路径中的项目 ID 所示)。为什么需要一个数组?这里只有一个结果。

标签: java android arrays json object


【解决方案1】:

更改JSONArray list = new JSONArray(Jo_result.getString("result"));

JSONObject list = new JSONObject(Jo_result.getString("result"));

您的字符串包含在 {} 之间,这使它成为一个对象。

记住这一点

{} = Json 对象

[] = Json 数组

更新

当你这样做时

JSONObject resultsObject = list.getJSONObject(i);

它期望主对象中有另一个对象,例如:

{  // <-- main object

   i : { <-- object failing
     // ...
   }

}

这就是代码所期望的,其中 i 来自循环,但不存在,除此之外,key 必须是字符串,例如

{  // <-- main object

   string : {
     // ...
   }

}

所以,我建议您:删除 for 循环,因为您不再需要它,并像 list.getString("data_id") 这样在 json 中调用您的变量,因为它是包含您的 list json 对象。

希望对你有帮助。

【讨论】:

  • 这听起来合乎逻辑,谢谢您的回复!我现在确实收到以下JSONObject resultsObject = list.getJSONObject(i); 说法的错误。 getJSONObject (java.lang.string) in JSONObject cannot be applied to (int)
  • 我编辑了我的答案。如果它有效,请接受它作为答案(该死的声誉:P)
【解决方案2】:

您尝试过改造吗?你的情况会容易得多。 它使用 GSON 处理 Get/Post 请求并将 JSON 反序列化为 Java 对象

square.github.io/retrofit/

【讨论】:

    猜你喜欢
    • 2016-06-07
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多