This is the JSON object printed to screen:
{"UserName":"myUsername","Address":"myAddress","Password":"myPassword"}
In the following code, what value goes in data_obj.get() & in obj.get() to retrieve UserName value myUsername from JSON Object?
JSONObject obj = (JSONObject) data_obj.get();
System.out.println(obj.get("UserName"));
I tried this
JSONObject obj = (JSONObject) data_obj.get(0);
//Get the required data using its key
System.out.println(obj.get("UserName"));
but I got null pointer and was expecting the data for UserName field equals myUsername.
You're probably performing a redundant step.
That's how the data you've shared can be parsed:
String jsonStr = """
{"UserName":"myUsername","Address":"myAddress","Password":"myPassword"}
""";
JSONObject jsonObject = new JSONObject(jsonStr);
System.out.println(jsonObject.get("UserName"));
System.out.println(jsonObject.get("Address"));
System.out.println(jsonObject.get("Password"));
Output:
myUsername
myAddress
myPassword
Related
Im trying to parse id from following string json:
postResponse :{"success":true,"response_json":"{\"status\":200,\"message\":\"Success\",\"data\":{\"id\":\"f71233gg12\"}}"}
my code looks like below:
System.out.println("postResponse :"+postResponse);
JSONObject responseJson = new JSONObject(postResponse);
JSONObject jsonObjectA = responseJson.getJSONObject("response_json");
JSONObject jsonObjectB = jsonObjectA.getJSONObject("data");
String the_pdf_id = jsonObjectB.get("id").toString();
i keep getting the error:
org.json.JSONException: JSONObject["response_json"] is not a JSONObject.
what could be the reason? any solution for that?
As you can see on your data, the content at key response_json is not a an object, but only a string, another JSON-encoded string
// response_json value is a string
{"success":true,"response_json":"{\"status\":200,\"message\":\"Success\",\"data\":{\"id\":\"f71233gg12\"}}"}
// response_json value is an object
{"success":true,"response_json": {"status":200,"message":"Success","data":{"id":"f71233gg12"}}}
You need a second parse level
JSONObject jsonObjectA = new JSONObject(responseJson.getJSONString("response_json"));
I want to get only a specific value out of all the nested objects. In the application I just need the msg 3 which is inside another object messages.
I have tried it using JSONObject but it is not working for nested object. However It is working with just a single object means root object .
INPUT - {"name":"lola","messages":{"msg 1":"msg 2","msg 3":"msg 4"},"age":22}
String s = sc.nextLine();
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(s);
System.out.println(json);
Object name = json.get("messages");
System.out.println(name);
JSONObject messageObject = (JSONObject) json.get("messages");
System.out.println(employeeObject);
//Get employee first name
String msg= (String) messageObject.get("msg3");
System.out.println(msg);
The Output:
{"msg 3":"msg 4","msg 1":"msg 2"}
{"msg 3":"msg 4","msg 1":"msg 2"}
null
The last nested object is not fetching in any way. Another thing is that the normal print of the string as a JSONObject gets changed. like the msg3 came before msg1.
In place of null - msg4 should be there.
Thanks in advance.
You are missing a space between "msg" and "3". By the way - you can do this easier, as such.
String s = sc.nextLine();
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(s);
System.out.println(json);
System.out.println(json.get("message").get("msg 3"));
I have a response from API that generates authorization token with some other attributes, I'm trying to extract the value of the token from the response which looks like below
{"access_token":"tokenvalue","scope":"somevalue","token_type":"Bearer","expires_in":value}
i tried JSON parse like below:
Myclass response = template.postForObject(url, requestEntity, Myclas.class);
JSONParser jsonParser = new JSONParser();
JSONObject obj = (JSONObject) jsonParser.parse(response);
String product = (String) jsonObject.get("access_token");
token = response;
}
Recieved error:
parse( )
in JSONParser cannot be applied
to
(java.lang.String)
With the line:
String product = (String) jsonObject.get("access_token");
You are trying to get a property called "access_token".
However if you look at your json you will see that there is no property called "access_token".
Perhaps you meant:
String product = (String) jsonObject.get("token");
Like said by Neng Liu before me, this JSON could be incorrect:
{"token":"tokenvalue","scope":"somevalue","token_type":"Bearer","expires_in": value }
Another thing is that JSONObject can receive the JSON format in its constructor JSONObject obj = new JSONObject(response) in your case and then use obj.get("your filed")
I have a JSON String that I am parsing, and I want to retrieve the value of the field "pr_num". I am getting this error:
JSONObject["pr_num"] is not a JSONArray.
My code is below:
JSONObject obj = new JSONObject(jsonString);
JSONArray data = obj.getJSONArray("pr_num");
JSONObject obj1= data.getJSONObject(0);
System.out.println("JSON CONTENTS ARE"+obj1.getString("pr_num"));
I want to get values for pr_num field which are 690052 and null.
jsonString is mention below
[{
"executed_by": "vishnuc",
"testplan_id": 17372,
"pr_num": "690052"
},
{
"executed_by": "kkavitha",
"testplan_id": 17372,
"pr_num": null
}]
your jsonString is a json formatted array
So first you have to get it to a json array (not to an json object)
JSONArray obj = new JSONArray(jsonString);
Now you can iterate through the obj array
for(int i=0;i<obj.length();i++){
System.out.println("content one: " + obj.getJSONObject(i).getString("pr_num"));
}
Hope this helps.
I am trying to get the name of my json key from my jsonarray but not able to take in java.
[{"ParamName":"param1","DefaultValue":"","Hidden":"Hidden","LinkedParameter":"city"}]
I just want to take name of key like i only want to take "ParamName" and need to check
if (ParamName == "ParamName") {
Then do this.
}
You can convert this to JSON object.
JSONArray json = (JSONArray)new JSONParser().parse("[{\"ParamName\":\"param1\",
\"DefaultValue\":\"\",\"Hidden\":\"Hidden\",\"LinkedParameter\":\"city\"}]");
JSONObject obj= (JSONObject) json.get(0);
if("ParamName".equals(obj.get("ParamName"))){
}