Create complex json objects using gson - java

How to create javabean for gson for the below JSON script?
{
"header": [
{
"title": {
"attempts": 3,
"required": true
}
},
{
"on": {
"next": "abcd",
"event": "continue"
}
},
{
"on": {
"next": "",
"event": "break"
}
}
]
}
I'm trying to build the javabean for this JSON output. I'm not able to repeat the fieldname on.
Please suggest any solutions.

You will need multiple classes to accomplish this. I made some assumptions with the naming, but these should suffice:
public class Response {
private List<Entry> header;
private class Entry {
private Title title;
private On on;
}
private class Title {
int attempts;
boolean required;
}
private class On {
String next, event;
}
}
You can test it with a main() method like:
public static void main(String[] args) {
// The JSON from your post
String json = "{\"header\":[{\"title\":{\"attempts\":3,\"required\":true}},{\"on\":{\"next\":\"abcd\",\"event\":\"continue\"}},{\"on\":{\"next\":\"\",\"event\":\"break\"}}]}";
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Response response = gson.fromJson(json, Response.class);
System.out.println(response.header.get(0).title.attempts); // 3
System.out.println(response.header.get(1).on.next); // abcd
System.out.println(gson.toJson(response)); // Produces the exact same JSON as the original
}

Related

Json string to Object mapping with dynamic values

I am consuming Thirdparty jsonString, I am trying to parse the json but sometimes JSON object "RadarReports" is an list and sometimes it object.
{"RadarReports": {
"executionTime": "135",
"RadarReport": {
"abc": "1116591",
"name": "abc",
"id": "2019050311582056119",
"ownerId": "xyz"
},
"size" :"1"
}}
=================
{"RadarReports": {
"executionTime": "113",
"RadarReport": [
{
"abc": "1116591",
"name": "abc",
"id": "2019050311582056119",
"ownerId": "xyz"
},
{
"abc": "1116591",
"name": "abc",
"id": "2019050311582056119",
"ownerId": "xyz"
},
]
"size" : "2"
}}
I tried below to parse but failing when single object came into picture, need to accept both single and list of objects.
#Data
public class Radarreports {
private int size;
private ArrayList<RadarreportSet> RadarReportSet;
private ArrayList<RadarReport> RadarReport;
}
#Data
public
class ReportsResponse {
Radarreports RadarReports;
}
URL url = new URL(queryUrl);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
Gson gson = new GsonBuilder().create();
ReportsResponse radarReports = gson.fromJson(br, ReportsResponse.class);
You could solve this with a custom TypeAdapterFactory which creates an adapter which first peeks at the type of the JSON data and then adds special handling where the JSON object is not wrapped in an JSON array:
// Only intended for usage with #JsonAdapter
class SingleObjectOrListAdapterFactory implements TypeAdapterFactory {
#Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
// Note: Cannot use getDelegateAdapter due to https://github.com/google/gson/issues/1028
TypeAdapter<T> listAdapterDelegate = gson.getAdapter(type);
TypeAdapter<JsonObject> jsonObjectAdapter = gson.getAdapter(JsonObject.class);
return new TypeAdapter<T>() {
#Override
public void write(JsonWriter out, T value) throws IOException {
listAdapterDelegate.write(out, value);
}
#Override
public T read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.BEGIN_OBJECT) {
// Wrap JSON object in a new JSON array before parsing it
JsonObject jsonObject = jsonObjectAdapter.read(in);
JsonArray jsonArray = new JsonArray();
jsonArray.add(jsonObject);
return listAdapterDelegate.fromJsonTree(jsonArray);
} else {
return listAdapterDelegate.read(in);
}
}
};
}
}
The factory can then be specified for the affected field with #JsonAdapter:
#JsonAdapter(SingleObjectOrListAdapterFactory.class)
private ArrayList<RadarReport> RadarReport;

GSON parser skipping Object while looping over json

I have a Json array that I parse to GSON. It has the following content:
[
{
"type": "way",
"id": 70215497,
"nodes": [
838418570,
838418571
]
},
{
"type": "way",
"id": 70215500,
"nodes": [
838418548,
838418626
]
}
]
I tried to parse it using the following sample of code:
while (jsonReader.hasNext()){
Element type = gson.fromJson(jsonReader, Element.class);
if (type.GetType().contentEquals("way")) {
Way way = gson.fromJson(jsonReader, Way.class);
System.out.println(way.GetId());
}
}
Where Element is simply
public class Element {
private String type;
public String GetType() {
return type;
}
}
and Way is
public class Way {
private long id;
private String type;
List<Long> nodes;
public long GetId() {
return id;
}
}
Now for some reason only 70215500 will print out. And this happens for a few other elements in the real code. Why is that ?
Edit: It basically only reads 1/2 object. Why?
You do not need to read Element class first and after that Way class. Read Way and check it's type:
try (JsonReader jsonReader = new JsonReader(new FileReader(jsonFile))) {
jsonReader.beginArray();
while (jsonReader.hasNext()) {
Way way = gson.fromJson(jsonReader, Way.class);
if (way.getType().contentEquals("way")) {
System.out.println(way.getId());
}
}
}
Above code should print all ids.

can Rest API parse hashmap<string,object>?

i would like to know if rest api while consuming input parameter can do the following:
let's say my json object have the following parameters:
string name;
string adress;
hashmap<string,object> content;
and here's an exemple of what can be sent:
{
"name": "AZ",
"adress": "US",
"content": {
"clients": [
{
"client_ref":"213",
"commands" : {
"subCommands": [
{
"num":"1",
"price":"10euro"
},
{
"num":"12,
"price":"10euro"
}
]
}
},
{
"client_ref":"213",
"commands" : {
"subCommands": [
{
"num":"1",
"price":"10euro"
},
{
"num":"12,
"price":"10euro"
}
]
}
}
]
}
}
the question is can rest build the hashmap where the object can itself have n child of hashmap type ... ?
(i'm using jersey as rest implementation )
Assuming that you have a JSON provider such as Jackson registered and your model class looks like:
public class Foo {
private String name;
private String address;
private Map<String, Object> content;
// Getters and setters
}
The following resource method:
#Path("foo")
public class Test {
#POST
#Consumes(MediaType.APPLICATION_JSON)
public Response post(Foo foo) {
...
}
}
Can handle a request like:
POST /api/foo HTTP/1.1
Host: example.org
Content-Type: application/json
{
"name": "AZ",
"adress": "US",
"content": {
"clients": [
{
"client_ref": "213",
"commands": {
"subCommands": [...]
}
},
{
"client_ref": "213",
"commands": {
"subCommands": [...]
}
}
]
}
}
content is an Object, not a map.
"content": {
"clients": [
{
"client_ref":"213",
"commands" : {
"subCommands": [
{
"num":"1",
"price":"10euro"
},
{
"num":"12,
"price":"10euro"
}
]
}
},
{
"client_ref":"213",
"commands" : {
"subCommands": [
{
"num":"1",
"price":"10euro"
},
{
"num":"12,
"price":"10euro"
}
]
}
}
]
}
And this is Java Object presentation.
public class Content {
private List<Client> clients;
//Getters and setters
}
public class Client {
private String clientRef;
private List<Command> commands;
//Getters and setters
}
//And so on, define other classes.
To answer your question, yes, you can build a map.
Check this example, please. It tells how to parse an unknown json (in case you don't know the exact structure of your json object).
https://stackoverflow.com/a/44331104/4587961
Then you can build a map with fields
Map<String, Object> where some values of this map will be nested maps.
you can use javax.ws.rs.core.GenericEntity to wrap collections with generic types (your HashMap).
#GET
#Path("/mapping")
#Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getAllMapContents() {
Map<String,Object> map = new HashMap<String,Object>();
map.put("Hello", "World");
map.put("employee", new Employee(1,"nomad"));
GenericEntity<Map<String,Object>> entity = new GenericEntity<Map<String,Object>>(map) {};
return Response.ok(entity).build();
}
I checked it and found it working Please find the response below. Thank you.
{
"Hello": "World",
"employee": {
"id": 1,
"name": "nomad"
}
}

How to get array of objects from JSON response using GSON

I am writing a REST client using RestTemplate and GSON.
Below is sample of my JSON response
{
"value": [
{
"properties": {
"vmId": "f7f953fb-d853-4373-b564-",
"hardwareProfile": {
"vmSize": "Standard_D2"
},
},
"name": "A",
"Id": ""
},
{
"properties": {
"vmId": "f7f953fb-d853-4373-b564-",
"hardwareProfile": {
"vmSize": "Standard_D2"
},
},
"name": "B",
"Id": ""
},
{
"properties": {
"vmId": "f7f953fb-d853-4373-b564-",
"hardwareProfile": {
"vmSize": "Standard_D2"
},
},
"name": "C",
"Id": ""
}
]
}
What I want to is that I want to get only the values for the property --> "name"
So I created a simple POJO that has only name as the member field.
public class VMNames {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
and I am trying to use the GSON like this to get a array of this POJO. Here, the response is my JSON response object.
Gson gson = new Gson();
VMNames[] vmNamesArray = gson.fromJson(response.getBody(), VMNames[].class);
System.out.println(vmNamesArray.length);
But when I do this, I get an error i.e. as below:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
Please note that I don't want to create a POJO that has exactly same structure as my JSON object because I want to get only one attribute out of my JSON object. I am hoping that I won't have to really create a POJO with the same structure as my JSON response because, in reality, it's a huge response and I don't control it, so it can also change tomorrow.
Can you try this:
public class VMNames {
#SerializedName("name")
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Type collectionType = new TypeToken<Collection<VMNames>>(){}.getType();
Collection<VMNames> vmNamesArray = gson.fromJson(response.getBody(), collectionType);
System.out.println(vmNamesArray.length);
or try:
VMNames[] vmNamesArray = gson.fromJson(response.getBody(), VMNames[].class);
So I could get this done. Posting the answer to this so that someone can be benefited tomorrow :)
First thing, i stopped using GSON and started using JSON.
And below is the code that helped.
RestTemplate restTemplate = new RestTemplate();
response = (ResponseEntity<String>) restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
JSONObject jsonObj = new JSONObject(response.getBody().toString());
JSONArray c = jsonObj.getJSONArray("value");
for (int i = 0; i < c.length(); i++) {
JSONObject obj = c.getJSONObject(i);
String VMName = obj.getString("name");
VMNames vmnames = new VMNames();
vmnames.setName(VMName);
vmNames.add(vmnames);
}
return vmNames;
And i get a list of all the value against the attribute name in form of a json array.

Deserialize JSON to POJO using Retrofit and Gson

I am working with an API that responds like the following for a single user resource:
{
"data": {
"id": 11,
"first_name": "First",
"last_name": "Last",
"books": {
"data": [
{
"id": 13,
"name": "Halo"
}
]
},
"games": {
"data": [
{
"id": 1,
"name": "Halo"
}
]
}
}
}
or like the following for multiple user resources:
{
"data": [
{
"id": 11,
"first_name": "First",
"last_name": "Last",
"books": {
"data": [
{
"id": 13,
"name": "Halo"
}
]
},
"games": {
"data": [
{
"id": 1,
"name": "Halo"
}
]
}
},
],
"meta": {
"pagination": {
"total": 11,
"count": 10,
"per_page": 10,
"current_page": 1,
"total_pages": 2,
"links": {
"next": "http://api.###.com/users?page=2"
}
}
}
}
Key things to notice are:
all resources are nested under a data key, single as an object or multiple as an array of objects. This includes nested resources such as books and games in the example above.
I need to be able retrieve the values of the meta key for my pagination routines
User model
public class User extends BaseModel {
public Integer id;
public String firstName;
public String lastName;
public List<Book> books; // These will not receive the deserialized
public List<Game> games; // JSON due to the parent data key
}
Custom JSON deserializer
public class ItemTypeAdapterFactory implements TypeAdapterFactory {
public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<T>() {
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
}
public T read(JsonReader in) throws IOException {
JsonElement jsonElement = elementAdapter.read(in);
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
// If the data key exists and is an object or array, unwrap it and return its contents
if (jsonObject.has("data") && (jsonObject.get("data").isJsonObject() || jsonObject.get("data").isJsonArray())) {
jsonElement = jsonObject.get("data");
}
}
return delegate.fromJsonTree(jsonElement);
}
}.nullSafe();
}
}
This is all working fine but I can't figure out how to access the meta key for pagination.
Ideally I would get Gson to deserialize the response to the following POJO:
public class ApiResponse {
public Object data;
public Meta meta
}
and I could just cast the response field to the correct type in the response callback like the following:
Map<String, String> params = new HashMap<String, String>();
params.put("include", "books,games");
ApiClient.getClient().authenticatedUser(params, new ApiClientCallback<ApiResponse>() {
#Override
public void failure(RestError restError) {
Log.d("TAG", restError.message);
}
#Override
public void success(ApiResponse response, Response rawResponse) {
User user = (User) response.data; // Cast data field to User type
Log.d("TAG", user.firstName);
Log.d("TAG", "Total pages" + response.meta.pagination.total.toString()); // Still have access to meta key data
}
});
However the data field of the ApiResponse object is null.
My Java is very rusty and I have no idea if this is even possible nor do I understand how to go about it correctly, any help would be much appreciated.
I needed the same thing and have managed to get it working by adding an if statement to your custom serializer:
…
// If the meta key exists, consider the element to be root and don't unwrap it
if (!jsonObject.has("meta")) {
// If the data key exists and is an object or array, unwrap it and return its contents
if (jsonObject.has("data") && (jsonObject.get("data").isJsonObject() || jsonObject.get("data").isJsonArray())) {
jsonElement = jsonObject.get("data");
}
}
…
The reason why the data field of your ApiResponse was null is because your original deserializer was processing the whole response and making you "loose" the root object's data and meta elements.
I've also parametized the ApiResponse class:
public class ApiResponse<T> {
public Meta meta;
public T data;
}
That way deserializing still works without creating many different Response classes, while casting isn't needed anymore and you can specify the type of ApiResponse's data field as needed (eg. ApiResponse<User> for single user resource, ApiResponse<List<User>> for multiple user resources, etc.).

Categories