Get a JSON object from a HTTP response
The string that you get is just the JSON Object.toString(). It means that you get the JSON object, but in a String format.
If you are supposed to get a JSON Object you can just put:
JSONObject myObject = new JSONObject(result);
how to get JSON value from HTTP response?
You can convert JSON string into JSONObject
and then get then token using getString()
method
JSONObject result = new JSONObject(EntityUtils.toString(entity));
String token = result.getString("token");
How do I get a JSON object from an Http Response?
As your image shows you are getting the response on console, try accessing it as follows,
.subscribe(response=>{
console.log(response['access_token']);
})
get json from HttpResponse
I think the problem you are running into is one similar to my own I just ran into. If you run:
String json_string = EntityUtils.toString(response.getEntity());
JSONObject temp1 = new JSONObject(json_string);
The above code will throw an exception and it looks like the JSON array brackets are to blame. But it's fine to have a JSON array as the top level element! You just need to use JSONArray() instead of JSONObject:
String json_string = EntityUtils.toString(response.getEntity());
JSONArray temp1 = new JSONArray(json_string);
So you have to know if you are getting a JSONArray or a single dictionary that is a JSONObject in your JSON code.
If you are used to the iOS/Objective-C JSON parsing libraries they use the same top level element to deal with json dictionaries and json array's, so moving to the JAVA / Android world confused me about having two types for handling JSON depending on the top level returned.
Parsing a JSON from HTTP Response in Java
Better and easier to use Gson
Gson gson = new Gson;
NameBean name = gson.fromJson(content.toString(),NameBean.class)
NameBean
is the object where you persist the json string.
public class NameBean implements Serializable{
public String name;
public String lastname;
public Int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Int getAge() {
return age;
}
public void setAge(Int age) {
this.age = age;
}
}
How to map HttpResponse in a Object Java
Another commonly used library for this is Jackson ObjectMapper. Using ObjectMapper, you can map the body to an object like this:
String json = response.body(); // "{ \"name\" : \"Nemo\", \"type\" : \"Fish\" }";
Animal nemo = objectMapper.readValue(json, Animal.class);
For a step by step guide, see https://www.baeldung.com/jackson-object-mapper-tutorial#2-json-to-java-object
Is there a way to extract JSON from an http response without having to build structs?
Golang: fetch JSON from an HTTP response without using structs as helpers
This is a typical scenario we come across. This is achieved by json.Unmarshal
.
Here is a simple json
{"textfield":"I'm a text.","num":1234,"list":[1,2,3]}
which is serialized to send across the network and unmarshaled at Golang end.
package main
import (
"fmt"
"encoding/json"
)
func main() {
// replace this by fetching actual response body
responseBody := `{"textfield":"I'm a text.","num":1234,"list":[1,2,3]}`
var data map[string]interface{}
err := json.Unmarshal([]byte(responseBody), &data)
if err != nil {
panic(err)
}
fmt.Println(data["list"])
fmt.Println(data["textfield"])
}
Hope this was helpful.
Related Topics
Java.Sql.Sqlexception: Column Count Doesn't Match Value Count at Row 1
Java.Lang.Illegalstateexception: Scanner Closed
Differencebetween Unidirectional and Bidirectional JPA and Hibernate Associations
Map.Clear() VS New Map:Which One Will Be Better
Registering a Headset Button Click with Broadcastreceiver in Android
How to Convert 24 Hr Format Time in to 12 Hr Format
Find First Element by Predicate
Javafx CSS Error ( Property Stylesheets Does Not Exist )
Javafx - How to Create a Thin Menubar
Javafx - What Is This Weird Container That Holds Context Menu
Mapstruct and Lombok Not Working Together
JPA Native Query Select and Cast Object
In Java, Are Enum Types Inside a Class Static