Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

Android java type org.json.JSONObject cannot be converted to JSONArray

I am trying to connect to an Api link, read and construct all values in a object list.

I call the AsyncTask from my activity. I checked the list values and they are all correct.

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);
    }
}

While trying to run this code I get a error on

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)

The error itself is quite self explaining but I dont know why its being called. This is one of the links which shows that it is an JSON array. I tried to add every property as a JSONObject as the error states but then it didn't know any of the structure properties.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

Your string is contained between {} which makes it an object.

Keep in mind this

{} = Json Object

[] = Json Array

UPDATE

When you do this

JSONObject resultsObject = list.getJSONObject(i);

it's expecting another object within the main object, for example :

{  // <-- main object

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

}

That's what is the code expecting, where i is from the loop, but that doesn't exist and besides that, the key must be a string, for example

{  // <-- main object

   string : {
     // ...
   }

}

So, what I recommend you : remove the for loop since you don't need it anymore and call your variables within the json like this list.getString("data_id") since it's the list which contains your json object.

Hope it helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...