I got this json object data which is
{
"data": {
"id": 2,
"email": "[email protected]",
"first_name": "Janet",
"last_name": "Weaver",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg"
},
"ad": {
"company": "StatusCode Weekly",
"url": "http://statuscode.org/",
"text": "A weekly newsletter focusing on software development, infrastructure, the server, performance, and the stack end of things."
}
}
I want to parse json which i want to print output email in object…I use org.simple.json library .
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("curl -s -S https://reqres.in/api/users/2");
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String result = br.readLine();
Object obj=JSONValue.parse(result);
How do I println email data via data -> email
5
Answers
Download dependency https://mvnrepository.com/artifact/org.json/json/20180813
and Use below code –
You can just cast the value to
JSONObject
and useJSONObject
API further for printingAccording the java doc,
JSONValue.parse
returnsIn your case it should be a
JSONObject
,so you can cast it to aJSONObject
and use method in JSONObject to retrive email.In recent version
JSONObject
has been deprecated, useJsonObject
instead, which don’t bother using so many casting.ObjectMapper can also be used:
use the pom to download the dependency.
Code:-
I think that in general case you have to use some
JSON
parser framework like Jackson. But in case you have to find only one value and do not care aboutjson
validation or other aspects, then you could use simpleRegExp
:or event simplier
str.indexOf()
: