2

My HTTP Request responds with combination of string and JSON, something like this:

null{"username:name","email:email"}

I need only the JSON part.

I directly tried parsing as json object, which was not right of course. I tried splitting it: serverResponse.split("{"), but android does not allow to parse with this character because it is not a pattern. Any suggestion how i can achieve this?

1

3 Answers 3

4

String.split uses regular expressions, and since '{' is a special character in regular expressions, you should escape it like this: serverResponse.split("\\{").

Sign up to request clarification or add additional context in comments.

Comments

3

It would be better to change the server side, but you can also just use split. The only thing you need to do is escape your {.

String json = serverResponse.split("\\{")[1];

2 Comments

I also don't know why the server side sends null because it is not handled by me. I just had to get json and parse it. Thank you for the helpful reply.
Probably the server sends JSONP, which is used for javascript, the null is actually the callback variable. Try to get the url like: http:yoururl.com/?callback= to see if that gives you an empty string in front.
1

It is a bad idea and a bad practice to split a Json. If one day it you change on the serve side, it may pick a wrong part of your Json Object. I recommend you to PARSE it, even if it is simple and small.

2 Comments

He does not want to split the json, he wants to remove the null in front.
Yes i wanted to separate the "null" and the json object which i succeed by serverResponse.split("\\{").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.