2012-12-13 3 views
0

this 링크를 JSON으로 구문 분석하려고하지만 항상 JSONOject로 변환 할 수 없습니다 오류가 발생합니다.JSONObject로 변환 할 수 없습니다.

문자열을 JSONObject로 변환하기 위해 많은 수의 샘플을 시도했지만 그 중 아무 것도 사용하지 않았습니다. Inputstream의 크기가이 정도의 데이터에 비해 더 적은지 아닌지 확실하지 않습니다.

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

inputstream을 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(); 

출력 문자열 :

{"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 

문제를 해결하기 위해 도와주세요. 감사합니다, 내가 다운로드이 AnyTask 클래스 Yuvi

+0

... – Sam

+0

포스트 코드 또는 무언가 :) –

+1

아마 그 인코딩 문제? json 데이터가 다음과 같이 설명하는 동안 'iso-8859-1'으로 읽는 중입니다. "인코딩": "UTF-8" – micha

답변

0

및 구문 분석 JSON

private List<HashMap<String, String>> mapList = new ArrayList<HashMap<String, String>>(); 

    private InputStream is; 

    @Override 
      protected List<HashMap<String, String>> doInBackground(String... urls) { 

       try { 
        DefaultHttpClient httpClient = getNewHttpClient(); 
        HttpGet get = new HttpGet((String) urls[0]); 
        HttpResponse httpResponse = httpClient.execute(get); 
        HttpEntity httpEntity = httpResponse.getEntity(); 

        is = httpEntity.getContent(); 

        BufferedReader reader = new BufferedReader(new InputStreamReader(
          is, "iso-8859-1")); 
        StringBuilder sb = new StringBuilder(); 
        String line = null; 
        while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
        } 
        is.close(); 
        JSONArray jsonArray = new JSONArray(sb.toString()); 
         for (int i = 0; i < jsonArray.length(); i++) { 
          JSONObject row = jsonArray.getJSONObject(i); 
          DowloadSpecificJson(row); 


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

       return mapList; 
      } 

    private void DowloadSpecificJson(JSONObject row) throws JSONException { 

     HashMap<String, String> map = new HashMap<String, String>(); 
map.put("id", row.getString("id")); 
      map.put("name", row.getString("name")); 
      map.put("img", row.getString("img")); 

mapList.add(map); 
} 
+0

시도해 보셨나요? 링크로, 나는 ..을 제공 했습니까? 이미 JSON 구문 분석을 수행하는 방법을 알고 있습니다 ... – Yuvi

0
String json = EntityUtils.toString(response.getEntity(), "UTF-8"); 
return new JSONObject(json); 
+0

위에 제공된 링크로 시도해보십시오. – Yuvi

0

나는 2.3에 같은 문제가 있지만, 4.0 괜찮아. 나는 저녁 내내 해결책을 찾고있었습니다. 그것은 매우 간단합니다 - URL에서 수신하는 문자열에서 첫 번째 심볼을 제거해야합니다.

당신을 도울 수 있도록 당신이 우리를 위해 당신의 관련 코드 및 로그 캣 오류를 게시 할 필요가
// Convert this response into a readable string 
String jsonString = StreamUtils.convertToString(response.getEntity().getContent()); 
jsonString = jsonString.substring(1); 
관련 문제