且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我应该怎么做来解决这个错误?

更新时间:2023-12-03 19:51:34

您JSON样本是组织结果的好办法:在结果对象混合的数量。假定数表示阵列或其他东西中的对象的数目

如果你可以假设这个数字永远是第一要素,并根据的那么它应该这样的工作方式,你可以尝试读取外循环数组的第​​一个值:

  =响应json.getJSONArray(TAG_RESPONSE);//从你的榜样,NUM将是50036:
NUM = response.getInt(0);的for(int i = 1; I< response.length();我++){
    JSONObject的C = response.getJSONObject(I)

注意,链接文件中的示例具有该号码作为一个字符串:

  {回应:
    [5,
     {援助:60830458,owner_id:6492,艺术家:NONAME,称号:黄宗泽
持续时间:195,URL:HTTP:\\ / \\ / cs40.vkontakte.ru ​​\\ / u06492 \\ /音频\\ /2ce49d2b88.mp3},
     {援助:59317035,owner_id:6492,艺术家:梅斯特Barrao,称号:Sinhazinha
持续时间:234,URL:HTTP:\\ / \\ / cs510.vkontakte.ru ​​\\ / u2082836 \\ /音频\\ /
d100f76cb84e.mp3}]}

JSONArray.getInt()将解析字符串 INT 为您服务。

和发现某些在你的数组中的对象的值也是数字,你可能需要阅读那些为 INT 也:

  INT援助= c.getInt(TAG_AID);
INT owner_id = c.getInt(TAG_OWNER_ID);
INT持续时间= c.getInt(TAG_DURATION);

I was using JSONParser to obtain results of a search, for that I followed this tutorial: http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

The thing is that, the API I am using gives the results like this:

{"response":[50036,{"aid":88131498,"owner_id":61775052,"artist":"Terror Squad","title":"Lean Back (OST Need For Speed Underground 2)","duration":249,"url":"http:\/\/cs4408.vkontakte.ru\/u59557424\/audio\/7f70f58bb9b8.mp3","lyrics_id":"3620730"},{"aid":106963458,"owner_id":-24764574,"artist":"«Dr. Dre ft Eminem, Skylar Grey (Assault Terror)","title":"I Need A Doctor (ASSAULT TERROR DUBSTEP REMIX)»","duration":240,"url":"http:\/\/cs5101.vkontakte.ru\/u79237547\/audio\/12cd12c7f8c2.mp3","lyrics_id":"10876670"}]}

My problem comes when I have to parse the first integer (here it is 50036) which is the number of results found.

I don't know how to read that integer.

This is my code:

private void instance(String artisttrack){
    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();

    // getting JSON string from URL
    String jsonurl = new String( "https://api.vk.com/method/audio.search?access_token=ACC_TOKEN&api_id=ID&sig=SIG&v=2.0&q=" + artistname + artisttrack + "&count=5");
    JSONObject json = jParser.getJSONFromUrl(jsonurl);

    try {
        // Getting Array of Contacts
        response = json.getJSONArray(TAG_RESPONSE);

        // looping through All Contacts
        for(int i = 0; i < response.length(); i++){
            JSONObject c = response.getJSONObject(i);

            // Storing each json item in variable
            //int results = Integer.parseInt(c.getString(TAG_RESULTS));
            String aid = c.getString(TAG_AID);
            String owner_id = c.getString(TAG_OWNER_ID);
            String artist = c.getString(TAG_ARTIST);
            String title = c.getString(TAG_TITLE);
            String duration = c.getString(TAG_DURATION);

            // Phone number is agin JSON Object
            //JSONObject phone = c.getJSONObject(TAG_PHONE);
            String url = c.getString(TAG_URL);
            String lyrics_id = c.getString(TAG_LYRICS_ID);
            Log.e("áaaaaaaaaaaaaaa", url);

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

}

The JSONParser.java is like written in the tutorial.

And here 2 lines of the logcat error:

W/System.err(10350): org.json.JSONException: Value 50036 at 0 of type java.lang.Integer cannot be converted to JSONObject
W/System.err(10350):    at org.json.JSON.typeMismatch(JSON.java:100)

Your JSON sample is a poor way to organize the results: mixing the number in with the result objects. Is the number supposed to indicate the number of objects in the array or something else?

If you can assume that this number will always be the first element, and according to this then it's supposed to work this way, you can try to read the first value of the array outside the loop:

response = json.getJSONArray(TAG_RESPONSE);

// from your example, num will be 50036:
num = response.getInt(0);

for (int i = 1; i < response.length(); i++){
    JSONObject c = response.getJSONObject(i);

Note that the example in the linked documentation has this number as a string:

{"response":
    ["5",
     {"aid":"60830458","owner_id":"6492","artist":"Noname","title":"Bosco",
"duration":"195","url":"http:\/\/cs40.vkontakte.ru\/u06492\/audio\/2ce49d2b88.mp3"},
     {"aid":"59317035","owner_id":"6492","artist":"Mestre Barrao","title":"Sinhazinha",
"duration":"234","url":"http:\/\/cs510.vkontakte.ru\/u2082836\/audio\/
d100f76cb84e.mp3"}]}

But JSONArray.getInt() will parse the String as an int for you.

And notice that some of the values in the objects in your array are also numbers, you may want to read those as int also:

int aid = c.getInt(TAG_AID);
int owner_id = c.getInt(TAG_OWNER_ID);
int duration = c.getInt(TAG_DURATION);