问题描述
我是用JSONParser获得搜索结果,对于我跟着这个教程:的
事情是这样的,我现在用的API给的结果是这样的:
{回应:[50036,{援助:88131498,owner_id:61775052,艺术家:恐怖队,称号:向后倾斜( OST极品飞车地下狂飙2)\",\"duration\":249,\"url\":\"http:\\/\\/cs4408.vkontakte.ru\\/u59557424\\/audio\\/7f70f58bb9b8.mp3\",\"lyrics_id\":\"3620730\"},{\"aid\":106963458,\"owner_id\":-24764574,\"artist\":\"«Dr.德瑞英尺阿姆,斯凯勒灰色(恐怖袭击),称号:我需要一个医生(突击TERROR DUBSTEP REMIX)»\",\"duration\":240,\"url\":\"http:\\/\\/cs5101.vkontakte.ru\\/u79237547\\/audio\\/12cd12c7f8c2.mp3\",\"lyrics_id\":\"10876670\"}]}
我的问题是当我要解析的第一个整数(这里是50036),这是发现的结果数量。
我不知道如何读的整数。
这是我的code:
私人无效实例(字符串artisttrack){
//创建JSON解析器实例
JSONParser jParser =新JSONParser(); // URL从获取JSON字符串
字符串jsonurl =新的String( \"https://api.vk.com/method/audio.search?access_token=ACC_TOKEN&api_id=ID&sig=SIG&v=2.0&q=\" + ARTISTNAME + artisttrack +&放大器;计数= 5);
JSONObject的JSON = jParser.getJSONFromUrl(jsonurl); 尝试{
//获取联系人的数组
响应= json.getJSONArray(TAG_RESPONSE); //通过所有联系人循环
的for(int i = 0; I< response.length();我++){
JSONObject的C = response.getJSONObject(I) //存储在变量中的每个JSON项目
// INT结果=的Integer.parseInt(c.getString(TAG_RESULTS));
字符串援助= c.getString(TAG_AID);
字符串owner_id = c.getString(TAG_OWNER_ID);
串艺术家= c.getString(TAG_ARTIST);
字符串title = c.getString(TAG_TITLE);
字符串长度= c.getString(TAG_DURATION); //电话号码是阿恩JSON对象
// JSONObject的手机= c.getJSONObject(TAG_PHONE);
字符串URL = c.getString(TAG_URL);
字符串lyrics_id = c.getString(TAG_LYRICS_ID);
Log.e(áaaaaaaaaaaaaaa,URL); }
}赶上(JSONException E){
e.printStackTrace();
}}
该JSONParser.java就像写教程。
在这里,2号线的logcat的错误:
W / System.err的(10350):org.json.JSONException:值50036的类型java.lang.Integer的0不能转换成的JSONObject
W / System.err的(10350):在org.json.JSON.typeMismatch(JSON.java:100)
您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);
这篇关于我应该怎么做来解决这个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!