0

I have string with JSON structure like this

String response = "{'success':1,'error_code':0,'message':'Access granted'}"

I want to split that using String.split() to be like this:

{[success],[1],[error_code],[0],[message],[Access Granted]}

I have tried this solution, this solution too, but none of solution fit with my need.

How I can achieve this?

7
  • 2
    Why do you want to split a valid JSON using String.split()? Commented Sep 12, 2014 at 11:04
  • @SeshuVinay its not a JSON, its string like JSON format. Commented Sep 12, 2014 at 11:07
  • If it's LIKE a JSON string, you can treat it as a JSON string. Commented Sep 12, 2014 at 11:12
  • @M42 okay, but how? give me a link or something. Commented Sep 12, 2014 at 11:13
  • JSONObject json=new JSONObject(string); Commented Sep 12, 2014 at 11:14

1 Answer 1

2

Try this way,hope this will help you to solve your problem.

try{
   JSONObject responseJson = new JSONObject("{\"success\":1,\"error_code\":0,\"message\":\"Access granted\"}");
   String valu1 = responseJson.getString("success");
   String valu2 = responseJson.getString("error_code");
   String valu3 = responseJson.getString("message");
}catch (Throwable e){
   e.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

4 Comments

And what about a comma in the value like: "message":"Access, granted"
@M42,is this kind of requirement his ask ?
I've updated my question, i think your response string was wrong. I need from String response = "{'success':1,'error_code':0,'message':'Access granted'}"
Not asked here but it may occur.

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.