4

I'm with problem to parse a JSON. Always that I try do it, the follow was result is returned: Unexpected character () at position 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. My response returns a JSON well formatted;
  2. The problem happens when this piece of code is running object = new JSONParser().parse(builder.toString());

The is part o my JSON file:

    [
   {
      "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
   }
]
All Pereira
  • 139
  • 1
  • 2
  • 13

5 Answers5

1

Most probably your content has some unprinted special character at beginning. For UTF-8 encoded data this may be a BOM.

Please post start of you content as byte[].

Aleh Maksimovich
  • 2,622
  • 8
  • 19
1

It is happening because of UTF-8 BOM. What is UTF-8 BOM ? The UTF-8 BOM is a sequence of bytes (EF BB BF) that allows the reader to identify a file as being encoded in UTF-8.

Normally, the BOM is used to signal the endianness of an encoding, but since endianness is irrelevant to UTF-8, the BOM is unnecessary.

How to solve the issue ? Convert encoding of your .json or any file to UTF-8 instead of UTF-8 BOM. Like this.

enter image description here

Om Sao
  • 7,064
  • 2
  • 47
  • 61
0

Use this instead of BufferReader

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

You will get your JsonData into Map and simply you can iterate over map.

KuldeeP ChoudharY
  • 446
  • 1
  • 6
  • 22
0

I believe your problem is with the content-type. Use something like this:

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

No need for BufferedReader, InputStreamReader can handle it fine. Hope it helps!

apexlol
  • 130
  • 11
0

I found the problem!

My JSON was returning a different space character, so I did add it in my code this::

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

This \uFEFF was my problem! And in my Dev environment it is not happens, just in production env!

All Pereira
  • 139
  • 1
  • 2
  • 13