1

I am trying to parse JSON from web service. But i get "String could not be converted to Json Object" error.

My json: {"encoding":"UTF-8","yorumlar":[["dogukan","deneme yorumu"],["Burak","yorum"]]} I get this string like below;

HttpClient client = new DefaultHttpClient();
            HttpUriRequest request = new HttpGet(url);
            HttpResponse response = null;
            try
            {
                response = client.execute(request);
                String jsonString = StreamUtils.convertToString(response.getEntity().getContent());
                JSONObject json = new JSONObject(jsonString);
                .
                .
                .

When i got this string from web service like above, i get this error but when i do that:

JsonObject object = new JsonObject("{\"encoding\":\"UTF-8\",\"yorumlar\":[[\"dogukan\",\"deneme yorumu\"],[\"Burak\",\"yorum\"]]}")

i don't get this error. So, the strings are the same and json format is ok. What is the problem?

serefakyuz
  • 273
  • 4
  • 14
  • 1
    did you debug and check what is "response" ? – Pararth May 30 '14 at 07:34
  • are you sure you are getting the correct response? have you logged it? – SMR May 30 '14 at 07:35
  • Hey, Please print log for response and put here for clerify doubt. M here to help you. – Brijesh Patel May 30 '14 at 07:40
  • Have you specified the **Content-Type** to **application/json** in your WS's answer ? – Quentin Klein May 30 '14 at 07:41
  • Yes, i debugged and copied the value of "jsonString". It was the same string. There is not any differences. @QuentinKlein Yes Content-Type is application/json – serefakyuz May 30 '14 at 07:48
  • Could there be some extra spaces in your string? Check this one: http://stackoverflow.com/questions/10267910/jsonexception-value-of-type-java-lang-string-cannot-be-converted-to-jsonobject – Raghu May 30 '14 at 08:33
  • check the value for variable jsonString also. Can try a normal convertToString with charset utf-8 [InputStreamReader(inputStream, "UTF-8")] – Pararth May 30 '14 at 08:33
  • I can get json from youtube gdata in same method. There is no problem while parsing youtube data. – serefakyuz May 30 '14 at 08:41
  • might be some character difference...compare the exact strings, encoding? – Pararth May 30 '14 at 08:49
  • I am going to be crazy. I have no idea what is the problem. I have tried changing encoding and the other ways but still gives error. Strings are the same, i can get json from youtube response in the same method. – serefakyuz May 30 '14 at 08:56
  • check all brackets, escape chars, post a logcat read of the time this happens – Pararth May 30 '14 at 11:04

1 Answers1

0

I solved the problem, the json string starts with these characters "". But these characters does not appear in debug mode. I created a java project. And i got the json string from server. I saw these characters in debug mode. Java project gave me more details about the converting error.

if you try to get the values of these in debug mode:

jsonString.toCharArray()[0] and jsonString.toCharArray()[1]

you will see the values as "", not "{". It's not space but does not appear. and "{" character located in 2. index.

We have solved the problem on server side. I guess that was an page encoding problem.

serefakyuz
  • 273
  • 4
  • 14