Android java类型org.json.JSONObject无法转换为JSONArray

时间:2015-05-28 21:43:30

标签: java android arrays json object

我正在尝试连接到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作为错误状态但是它不知道任何结构属性。

2 个答案:

答案 0 :(得分:2)

更改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 来自循环,但不存在,除此之外,必须是一个字符串,例如

{  // <-- main object

   string : {
     // ...
   }

}

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

希望它有所帮助。

答案 1 :(得分:0)

你尝试过改造吗?对你来说这样会容易得多。 它处理Get / Post请求&amp;使用GSON将JSON反序列化为Java对象

square.github.io/retrofit/