2017-12-14 9 views
1

JSONを解析するのに問題があります。いつも私はそれを行う試みること、フォローは結果が返されました:予期しない文字()の位置に0JSONParserが位置0に予期しない文字()を返します

public Object execute(HttpRequestBase request){ 
    DefaultHttpClient client = new DefaultHttpClient(); 
    HttpResponse response = null; 
    Object object = null; 

    try { 
     response = client.execute(request); 
     InputStream is = response.getEntity().getContent(); 
     BufferedReader br = new BufferedReader(new InputStreamReader((is))); 
     StringBuilder builder = new StringBuilder(); 
     String output; 

     while ((output = br.readLine()) != null) { 
      builder.append(output).append("\n");  
     } 

     if (response.getStatusLine().getStatusCode() == 200) { 
      object = new JSONParser().parse(builder.toString()); 
      client.getConnectionManager().shutdown(); 
     } else { 
      LOG.log(Level.SEVERE, builder.toString()); 
      throw new RuntimeException(builder.toString()); 
     } 

    } catch (IOException | ParseException ex) { 
     LOG.log(Level.SEVERE, ex.toString()); 
    } finally { 

    } 

    return object; 
} 

PS:

  1. 私の応答がうまくフォーマットされたJSONを返します。
  2. コードのこの作品は、object = new JSONParser().parse(builder.toString());

を実行しているときに問題が発生したザ・私のJSONファイルoを一部です:

[ 
    { 
     "id":2115, 
     "identificacao":"17\/2454634-6", 
     "ultima_atualizacao":null 
    }, 
    { 
     "id":2251, 
     "identificacao":"17\/3052383-2", 
     "ultima_atualizacao":"2017-11-21" 
    }, 
    { 
     "id":2258, 
     "identificacao":"17\/3070024-6", 
     "ultima_atualizacao":null 
    }, 
    { 
     "id":2257, 
     "identificacao":"17\/3070453-5", 
     "ultima_atualizacao":null 
    } 
] 
+0

'builder'の内容を解析する前に表示できますか?なぜ私は改行を追加しているのか理解できませんが、デバッグの観点から見ると分かります。 – stdunbar

+1

JSONはどこですか? – Eric

+0

JSON入力を調べるには、おそらくUTF-8 BOMで始まります。これは解析する前に取り除く必要があります。 –

答えて

1

おそらく、あなたのコンテンツが先頭にいくつかの印刷されていない特殊文字を持っています。 UTF-8でエンコードされたデータの場合、これはBOMである可能性があります。

コンテンツの開始をbyte[]としてください。

+0

私の問題は "\\ uFEFF"文字でした。 :( –

+0

予想通りのBOM) –

+0

はい! BOM!私のコードに変更を加えた後。 –

0

使用この代わりのBufferReader

Map<String,Object> result 
      = (Map<String,Object>)JSONValue.parse(IOUtils.toString(response.getEntity().getContent(), "utf-8")); 

あなたは地図にあなたのJsonDataを取得し、単にあなたがマップを反復処理することができます。

+0

インポートする必要があります import org.json.simple.JSONObject; import org.json.simple.JSONValue; –

+0

私はそれをやろうとしましたが、問題はBOMにありました。ありがとうございました。 –

0

あなたの問題はcontent-typeにあると思います。

HttpEntity content = response.getEntity(); 
StringBuilder sb = new StringBuilder(); 
InputStream is = content.getContent(); 
InputStreamReader isr = new InputStreamReader(is, "UTF-8"); 
int character; 
do { 
    character = isr.read(); 
    if (character >= 0) { 
     sb.append((char) character); 
    } 
} while (character >= 0); 
return sb.toString(); 

InputReadReaderはこれをうまく処理できません。 お手伝い願います!

+0

私はあなたが書くようにしましたが、うまくいきません! :(ありがとう、あなたは助けて! –

0

問題が見つかりました。私のJSONが異なる空白文字を返していましたので、私は私のコードでこれを、それを追加しました::

String content = builder.toString(); 
content = content.replaceAll("\\uFEFF", ""); 

この\ uFEFF

は私の問題でした!そして私の開発環境では、それは起こらない、ちょうど生産環境で!

関連する問題