问题描述
我试图解析<一个href=\"http://gdata.youtube.com/feeds/api/playlists/2B3B8DDAE8F71C46?v%E2%80%8C%E2%80%8B=2&alt=json\"相对=nofollow>这个链接作为JSON,但总是得到错误不能转换成以JSONOject。
我已经试过样本数量,以字符串转换为JSONObject的,但其中任何一个没有运气。我不知道InputStream的大小是否少这么多的数据或没有。
HttpClient的客户端=新DefaultHttpClient();
//执行GET请求到YouTube为所有的一个JSON列表
//由特定用户视频 // URI uri_ =新的URI(https://gdata.youtube.com/feeds/api/playlists/2B3B8DDAE8F71C46?v = 2及ALT = JSON);
HttpUriRequest要求=新HttpGet(\"https://gdata.youtube.com/feeds/api/playlists/2B3B8DDAE8F71C46?v%E2%80%8C%E2%80%8B=2&alt=json\");
//获取响应了YouTube发回
HTT presponse响应= client.execute(请求);
//转换这种反应成可读的字符串
串jsonString = StreamUtils.convertToString(response.getEntity()的getContent()); 的System.out.println(JSON:+ jsonString);
//创建我们可以从字符串使用JSON对象
JSONObject的JSON =新的JSONObject(jsonString);
code到的InputStream转换成字符串;
的BufferedReader读者=新的BufferedReader(新的InputStreamReader(
InputStream的,ISO-8859-1),8);
StringBuilder的SB =新的StringBuilder();
串线= NULL;
而((行= reader.readLine())!= NULL){
sb.append(行+\\ n);
}
inputStream.close();
输出字符串:
<$p$p><$c$c>{\"version\":\"1.0\",\"encoding\":\"UTF-8\",\"feed\":{\"xmlns\":\"http://www.w3.org/2005/Atom\",\"xmlns$media\":\"http://search.yahoo.com/mrss/\",\"xmlns$openSearch\":\"http://a9.com/-/spec/opensearchrss/1.0/\",\"xmlns$gd\":\"http://schemas.google.com/g/2005\",\"xmlns$yt\":\"http://gdata.youtube.com/schemas/2007\",\"id\":{\"$t\":\"http://gdata.youtube.com/feeds/api/playlists/2B3B8DDAE8F71C46\"},\"updated\":{\"$t\":\"2012-11-22T17:11:53.000Z\"},\"category\":[{\"scheme\":\"http://schemas.google.com/g/2005#kind\",\"term\":\"http://gdata.youtube.com/schemas/2007#playlist\"},{\"scheme\":\"http://gdata.youtube.com/schemas/2007/tags.cat\",\"term\":\"Bartle\"},{\"scheme\":\"http://gdata.youtube.com/schemas/2007/tags.cat\",\"term\":\"Doo\"},{\"scheme\":\"http://gdata.youtube.com/schemas/2007/tags.cat\",\"term\":\"Mumbles\"}],\"title\":{\"$t\":\"Mumbles\",\"type\":\"text\"},\"subtitle\":{\"$t\":\"Bartle Doo!\",\"type\":\"text\"},\"logo\":{\"$t\":\"http://www.youtube.com/img/pic_youtubelogo_123x63.gif\"},\"link\":[{\"rel\":\"alternate\",\"type\":\"text/html\",\"href\":\"https://www.youtube.com/playlist?list=PL2B3B8DDAE8F71C46\"},{\"rel\":\"http://schemas.google.com/g/2005#feed\",\"type\":\"application/atom+xml\",\"href\":\"https://gdata.youtube.com/feeds/api/playlists/2B3B8DDAE8F71C46\"},{\"rel\":\"http://schemas.google.com/g/2005#batch\",\"type\":\"application/atom+xml\",\"href\":\"https://gdata.youtube.com/feeds/api/playlists/2B3B8DDAE8F71C46/batch\"},{\"rel\":\"self\",\"type\":\"application/atom+xml\",\"href\":\"https://gdata.youtube.com/feeds/api/playlists/2B3B8DDAE8F71C46?alt=json&start-index=1&max-results=25\"}],\"author\":[{\"name\":{\"$t\":\"edbassmaster\"},\"uri\":{\"$t\":\"https://gdata.youtube.com/feeds/api/users/edbassmaster\"}}],\"generator\":{\"$t\":\"YouTube数据API\",\"version\":\"2.1\",\"uri\":\"http://gdata.youtube.com\"},\"openSearch$totalResults\":{\"$t\":25},\"openSearch$startIndex\":{\"$t\":1},\"openSearch$itemsPerPage\":{\"$t\":25},\"media$group\":{\"media$content\":[{\"url\":\"http://www.youtube.com/p/PL2B3B8DDAE8F71C46\",\"type\":\"application/x-shockwave-flash\",\"yt$format\":5}],\"media$description\":{\"$t\":\"Bartle Doo!\",\"type\":\"plain\"},\"media$thumbnail\":[{\"url\":\"http://i.ytimg.com/vi/6S7zwXGBiTE/default.jpg\",\"height\":90,\"width\":120,\"yt$name\":\"default\"},{\"url\":\"http://i.ytimg.com/vi/6S7zwXGBiTE/mqdefault.jpg\",\"height\":180,\"width\":320,\"yt$name\":\"mqdefault\"},{\"url\":\"http://i.ytimg.com/vi/6S7zwXGBiTE/hqdefault.jpg\",\"height\":360,\"width\":480,\"yt$name\":\"hqdefault\"}],\"media$title\":{\"$t\":\"Mumbles\",\"type\":\"plain\"}},\"yt$playlistId\":{\"$t\":\"PL2B3B8DDAE8F71C46\"},\"entry\":[{\"id\":{\"$t\":\"http://gdata.youtube.com/feeds/api/playlists/2B3B8DDAE8F71C46/PLaFx1lHMY9-c3iy7KngIxLbSSS_9OLXMd\"},\"published\":{\"$t\":\"2011-07-21T15:14:04.000Z\"},\"updated\":{\"$t\":\"1970-01-01T00:00:00.000Z\"},\"category\":[{\"scheme\":\"http://schemas.google.com/g/2005#kind\",\"term\":\"http://gdata.youtube.com/schemas/2007#video\"},{\"scheme\":\"http://gdata.youtube.com/schemas/2007/categories.cat\",\"term\":\"Comedy\",\"label\":\"Comedy\"}],\"title\":{\"$t\":\"Mumbles在Bassmaster经典,类型:文本},内容:{$ T:我的官方脸书:\\ r \\ nhttp://www.facebook.com/pages/Ed-Bassmaster/ 259411429706 \\ r \\ n \\ r \\ n我牛逼Shirts:\\r\http://www.districtlines.com/ed-bassmaster\",\"type\":\"text\"},\"link\":[{\"rel\":\"alternate\",\"type\":\"text/html\",\"href\":\"https://www.youtube.com/watch?v=6S7zwXGBiTE&feature=youtube_gdata\"},{\"rel\":\"http://gdata.youtube.com/schemas/2007#video.responses\",\"type\":\"application/atom+xml\",\"href\":\"https://gdata.youtube.com/feeds/api/videos/6S7zwXGBiTE/responses\"},{\"rel\":\"http://gdata.youtube.com/schemas/2007#video.related\",\"type\":\"application/atom+xml\",\"href\":\"https://gdata.youtube.com/feeds/api/videos/6S7zwXGBiTE/related\"},{\"rel\":\"http://gdata.youtube.com/schemas/2007#mobile\",\"type\":\"text/html\",\"href\":\"https://m.youtube.com/details?v=6S7zwXGBiTE\"},{\"rel\":\"related\",\"type\":\"application/atom+xml\",\"href\":\"https://gdata.youtube.com/feeds/api/videos/6S7zwXGBiTE\"},{\"rel\":\"self\",\"type\":\"application/atom+xml\",\"href\":\"https://gdata.youtube.com/feeds/api/playlists/2B3B8DDAE8F71C46/PLaFx1lHMY9-c3iy7KngIxLbSSS_9OLXMd\"}],\"author\":[{\"name\":{\"$t\":\"edbassmaster\"},\"uri\":{\"$t\":\"https://gdata.youtube.com/feeds/api/users/edbassmaster\"}}],\"gd$comments\":{\"gd$fee
请帮我解决这个问题。
谢谢,
Yuvi
我有2.3同样的问题,但在4.0它的罚款。
我整晚都在寻找解决方案。这是pretty简单 - 需要移除的字符串第一个符号,从URL接受
//转换这种反应成可读的字符串
串jsonString = StreamUtils.convertToString(response.getEntity()的getContent());
jsonString = jsonString.substring(1);
I am trying to parse this link as a JSON, but always getting the error Cannot converted into to JSONOject.
I have tried number of samples, to convert string to JSONObject, but no luck with any of them. I am not sure whether the size of Inputstream is less for this much data or not.
HttpClient client = new DefaultHttpClient();
// Perform a GET request to YouTube for a JSON list of all the
// videos by a specific user
//URI uri_ = new URI("https://gdata.youtube.com/feeds/api/playlists/2B3B8DDAE8F71C46?v=2&alt=json");
HttpUriRequest request = new HttpGet("https://gdata.youtube.com/feeds/api/playlists/2B3B8DDAE8F71C46?v%E2%80%8C%E2%80%8B=2&alt=json");
// Get the response that YouTube sends back
HttpResponse response = client.execute(request);
// Convert this response into a readable string
String jsonString = StreamUtils.convertToString(response.getEntity().getContent());
System.out.println("JSON:" + jsonString );
// Create a JSON object that we can use from the String
JSONObject json = new JSONObject(jsonString);
Code to convert inputstream into string;
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
inputStream.close();
Output string :
{"version":"1.0","encoding":"UTF-8","feed":{"xmlns":"http://www.w3.org/2005/Atom","xmlns$media":"http://search.yahoo.com/mrss/","xmlns$openSearch":"http://a9.com/-/spec/opensearchrss/1.0/","xmlns$gd":"http://schemas.google.com/g/2005","xmlns$yt":"http://gdata.youtube.com/schemas/2007","id":{"$t":"http://gdata.youtube.com/feeds/api/playlists/2B3B8DDAE8F71C46"},"updated":{"$t":"2012-11-22T17:11:53.000Z"},"category":[{"scheme":"http://schemas.google.com/g/2005#kind","term":"http://gdata.youtube.com/schemas/2007#playlist"},{"scheme":"http://gdata.youtube.com/schemas/2007/tags.cat","term":"Bartle"},{"scheme":"http://gdata.youtube.com/schemas/2007/tags.cat","term":"Doo"},{"scheme":"http://gdata.youtube.com/schemas/2007/tags.cat","term":"Mumbles"}],"title":{"$t":"Mumbles","type":"text"},"subtitle":{"$t":"Bartle Doo!","type":"text"},"logo":{"$t":"http://www.youtube.com/img/pic_youtubelogo_123x63.gif"},"link":[{"rel":"alternate","type":"text/html","href":"https://www.youtube.com/playlist?list=PL2B3B8DDAE8F71C46"},{"rel":"http://schemas.google.com/g/2005#feed","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/playlists/2B3B8DDAE8F71C46"},{"rel":"http://schemas.google.com/g/2005#batch","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/playlists/2B3B8DDAE8F71C46/batch"},{"rel":"self","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/playlists/2B3B8DDAE8F71C46?alt=json&start-index=1&max-results=25"}],"author":[{"name":{"$t":"edbassmaster"},"uri":{"$t":"https://gdata.youtube.com/feeds/api/users/edbassmaster"}}],"generator":{"$t":"YouTube data API","version":"2.1","uri":"http://gdata.youtube.com"},"openSearch$totalResults":{"$t":25},"openSearch$startIndex":{"$t":1},"openSearch$itemsPerPage":{"$t":25},"media$group":{"media$content":[{"url":"http://www.youtube.com/p/PL2B3B8DDAE8F71C46","type":"application/x-shockwave-flash","yt$format":5}],"media$description":{"$t":"Bartle Doo!","type":"plain"},"media$thumbnail":[{"url":"http://i.ytimg.com/vi/6S7zwXGBiTE/default.jpg","height":90,"width":120,"yt$name":"default"},{"url":"http://i.ytimg.com/vi/6S7zwXGBiTE/mqdefault.jpg","height":180,"width":320,"yt$name":"mqdefault"},{"url":"http://i.ytimg.com/vi/6S7zwXGBiTE/hqdefault.jpg","height":360,"width":480,"yt$name":"hqdefault"}],"media$title":{"$t":"Mumbles","type":"plain"}},"yt$playlistId":{"$t":"PL2B3B8DDAE8F71C46"},"entry":[{"id":{"$t":"http://gdata.youtube.com/feeds/api/playlists/2B3B8DDAE8F71C46/PLaFx1lHMY9-c3iy7KngIxLbSSS_9OLXMd"},"published":{"$t":"2011-07-21T15:14:04.000Z"},"updated":{"$t":"1970-01-01T00:00:00.000Z"},"category":[{"scheme":"http://schemas.google.com/g/2005#kind","term":"http://gdata.youtube.com/schemas/2007#video"},{"scheme":"http://gdata.youtube.com/schemas/2007/categories.cat","term":"Comedy","label":"Comedy"}],"title":{"$t":"Mumbles at the Bassmaster Classic","type":"text"},"content":{"$t":"My official Face Book:\r\nhttp://www.facebook.com/pages/Ed-Bassmaster/259411429706\r\n\r\nMy T Shirts:\r\nhttp://www.districtlines.com/ed-bassmaster","type":"text"},"link":[{"rel":"alternate","type":"text/html","href":"https://www.youtube.com/watch?v=6S7zwXGBiTE&feature=youtube_gdata"},{"rel":"http://gdata.youtube.com/schemas/2007#video.responses","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/videos/6S7zwXGBiTE/responses"},{"rel":"http://gdata.youtube.com/schemas/2007#video.related","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/videos/6S7zwXGBiTE/related"},{"rel":"http://gdata.youtube.com/schemas/2007#mobile","type":"text/html","href":"https://m.youtube.com/details?v=6S7zwXGBiTE"},{"rel":"related","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/videos/6S7zwXGBiTE"},{"rel":"self","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/playlists/2B3B8DDAE8F71C46/PLaFx1lHMY9-c3iy7KngIxLbSSS_9OLXMd"}],"author":[{"name":{"$t":"edbassmaster"},"uri":{"$t":"https://gdata.youtube.com/feeds/api/users/edbassmaster"}}],"gd$comments":{"gd$fee
Please help me to resolve the issue.Thanks,Yuvi
I have the same problem on 2.3, but on 4.0 it's fine.I was looking for solution all evening. It's pretty simple - need remove first symbol from string, that receive from url.
// Convert this response into a readable string
String jsonString = StreamUtils.convertToString(response.getEntity().getContent());
jsonString = jsonString.substring(1);
这篇关于不能转换成的JSONObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!