String cannot be converted to jsonobject

Newbie to Json here, I’m gettin a Value Database of type java.lang.String cannot be converted to JSONObject error when I try and run the following code.I cant make any sense of other answers on here.

This is the error

2 Answers 2

The you can parse it like here,

UPDATE :

If your Json is as you commented then you can easily retrieve this like

Your json is invalid. You can check your json on jsonlint.com Correct json should be like this <"server_response":<"code":"reg_true","message":"Sucessful registration.Thank you.Enjoy">> Since you are using only one json object there is no need to create json array.

Remember if it starts with [ and ends with ] its json array; with curly braces <. >its json object.

Not the answer you’re looking for? Browse other questions tagged android json or ask your own question.

Linked

Related

Hot Network Questions

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2020.1.17.35809

I have a JSON file with 2 JSON-Arrays in it: One Array for routes and one Array for sights.

A route should consist of several sights where the user gets navigated to. Unfortunately I am getting the error:

JSONException: Value of type java.lang.String cannot be converted to JSONObject

Читайте также:  Cin sync c что это

Here are my variables and the code that parses the JSON-File:

Log.i("JSON Parser", json); shows me that at the beginning of the generated string there is a strange sign:

but the error happens here:

04-22 14:01:05.043: E/JSON Parser(5868): Error parsing data org.json.JSONException: Value //STRANGE SIGN HERE // of type java.lang.String cannot be converted to JSONObject

anybody has a clue on how to get rid of these signs in order to create the JSONObject?

My server returns a JSON object via the body of an HTTP POST response, but I get the this error when my app tries to convert the string into a JSONObject:

It looks like my server is returning a acceptable JSON encoded string, but it just won’t convert to a JSONObject. I even changed the content-type of the server’s response header to "application/json". Please help me fix this, I’ve been trying all day.

EDIT- I use the following code:

I also tried imran khan’s suggestion:

4 Answers 4

How are you doing it? It should work with:

You can convert string to json as:

I FIXED IT! It was entirely my server’s fault. It turned out that my server was responding incorrectly. What happened was there was a bug within the web framework and after updating to the latest version, the problem solved itself. I’m guessing the old version of the web framework returned the incorrect content-type response header, or used some weird encoding.

So everyone’s Java code here should be 100% correct, because Java was not at fault here. THANKS FOR ALL YOUR EFFORT!

Miguel’s answer was the closest explanation, so I will accept his answer.

Читайте также:  Css подчеркнутый текст пунктиром

Not the answer you’re looking for? Browse other questions tagged java android json http or ask your own question.

Related

Hot Network Questions

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2020.1.17.35809

Rate this post

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *