Reputation:
This is the response.
{
"code": 200,
"data": {
"createdAt": "2019-12-09 15:21:07.0",
"id": 3,
"title": "{\"v\":\"1\"}",
"token": "INACTIVE"
},
"message": "SUCCESS"
}
I need value of v which is 1. I'm getting this
{"v":"1"}
through
JSONObject r2 = new JSONObject(operation);
String title = r2.getString("title");
System.out.println(title);
How do I get the value of v
after this? It would be great if I could get any suggestions.
Upvotes: 0
Views: 88
Reputation: 4088
String v= r2.getJSONObject("title").getString("v");
System.out.println(v);
PS: Don't forget to handle nulls,etc.
UPDATE: Please refer Andreas's answer as it is the correct answer. This answer wrongly assumes that title
also contains json.
Upvotes: 1
Reputation: 159086
The value of title
is a string with JSON text, so you need to re-invoke the JSON parser.
Also, in the question code, you forgot to navigate past the data
node.
JSONObject rootObj = new JSONObject(operation); // parse JSON text
String title = rootObj.getJSONObject("data").getString("title"); // get "title" value
JSONObject titleObj = new JSONObject(title); // parse JSON text
String v = titleObj.getString("v"); // get "v" value
System.out.println(v); // prints: 1
Upvotes: 2