I have a json like this:
"subject": {
"category": [
{
"name": "name1"
},
{
"name": "name2"
},
{
"name": "name3"
},
{
"name": "name4"
}
]
}
So it is an object containing a name array. What could be an equivalent Pojo for this?
Should I create a Subject object which has a string list called category?
I tried this:
public class Subject {
#JsonProperty(value = "category")
private List<String> name;
//getter setter ...
}
But I get nested exception: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
Every object should ideally be a class and every array a collection in Java.
It should be:
public class Subject {
private List<Category> category;
//getters and setters
}
class Category{
String name;
//getters and setters
}
yes you are right ..you can implement POJO like you mentioned
class Subject{
List<String> categories;
// getters and setters
}
In this json file you have Subject object containing one Array of objects "category" if you need values from that Subject object you should extract like this
console.log(Subject.category[0].name);
Why don't you just use Gson. Its pretty solid and well tested.
Subject subject = new Gson().fromJson(jsonObject.toString(), Subject.class);
You can also try this if Gson isn't an option here
public class Subject{
private List<Category> category;
//setter/getter(s)
private static class Category{
String name;
}
}
In addition to dev8080 answer I would suggest using jsonschema2pojo service.
If you paste there your source JSON and select Source type: JSON you will have the following output:
---------------com.example.Subject.java---------------
package com.example;
import java.util.ArrayList;
import java.util.List;
import javax.validation.Valid;
public class Subject {
#Valid
private List<Category> category = new ArrayList<Category>();
public List<Category> getCategory() {
return category;
}
public void setCategory(List<Category> category) {
this.category = category;
}
}
---------------com.example.Category.java---------------
package com.example;
public class Category {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
And you can easily tweak it using options or adopt some parts by hand.
Related
I'm trying to deserialize a JSON object using Jackson annotation, but I can't deserialize it:
Is an array of a type "Deposito"
{
"depositos": [
{
"deposito": {
"id": "13168775373",
"nome": "Geral",
"saldo": "100000.0000000000",
"desconsiderar": "N",
"saldoVirtual": "100000.0000000000"
}
}
]
}
my java class:
#JsonUnwrapped
#JsonProperty(value ="depositos")
private List<Deposito> depositos;
my deposito class:
#JsonRootName(value = "deposito")
public class Deposito {
private String id;
private String nome;
private Double saldo;
private String desconsiderar;
private Double saldoVirtual;
}
You would need to add an additional class to your model:
public class DepositoMetadata {
private Deposito deposito;
}
Now you need to adjust your main java class (as you called it):
private List<DepositoMetadata> depositos;
Finally, you can remove #JsonRootName(value = "deposito") from your Deposito class.
Is there any way for the JSON response to be formed in the same order in which I declare the attributes in the POJO?
If possible I would like to keep the POJOS as simple as possible, without annotations and things like that (like #JsonPropertyOrder, or #XmlType propOrder, ...)
If this is my POJO:
public class Header {
private String id;
private String date;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
I want the response to be:
{
"header": {
"id": "1",
"date": "20191031223016",
"name": "SAMPL"
}
}
... not this:
{
"header": {
"date": "20191031223016",
"id": "1",
"name": "SAMPL"
}
}
JSON has by definition in it's language spec unordered keys. That means there is no differentiation between JSON objects with it's keys in different orders.
If you require ordered data, you should wrap the members into a JSON list in a structure like so:
[
{"key": "id", "value": 1"},
{"key" ....
]
which also isn't really the purpose of JSON. You should accept the keys in any order in any case.
In that case, according to my taste, the cleanest solution would be this:
import javax.xml.bind.annotation.XmlType;
#XmlType(propOrder = {"id", "date", "name"})
public class Header {
}
I know there are lots of queries on this topic but nothing has been helpful for me to resolve below issue
{
"_embedded": {
"customers": [
{
"id": 101,
"name": "John",
"city": "Ohio"
},
{
"id": 102,
"name": "Tom",
"city": "London"
}
]
}
}
for this I have created below Java objects:
#Data
public class Wrapper {
#JsonProperty("_embedded")
private Customers customer;
}
#Data
public class Customers {
#JsonProperty("customer")
private List<Foo> obj;
}
#Data
#JsonInclude(JsonInclude.Include.NON_NULL)
public class Foo{
private int id;
private String name;
private String city;
}
Any help will be greatly appreciated.
You have some naming issues in your original question, but disregarding that, you could structure your classes according to the JSON to make it easier for both yourself and Gson.
Something like this would work:
public class JsonWrapper {
public Embedded _embedded;
}
public class Embedded {
public Customers customers;
}
public class Customers extends ArrayList<Foo>{ }
public class Foo{
public int id;
public String name;
public String city;
}
String json = "{\"_embedded\":{\"customers\":[{\"id\":101,\"name\":\"John\",\"city\":\"Ohio\"},{\"id\":102,\"name\":\"Tom\",\"city\":\"London\"}]}}";
JsonWrapper wrapper = new Gson().fromJson(json, JsonWrapper.class);
I have JSON that I get using retrofit
[{
"id":"1",
"name":"name",
"fields_json":{
"f1":"value1",
"f2":"value2",
"f3":"value3"
}
},{
"id":"2",
"name":"name2",
"fields_json":{
"f1":"value1",
"f2":"value2",
"f3":"value3"
}
}]
I want get List of Person(long id, String name, Map fields)
How I may get fields_json as Map in this class?
If you change your json a bit, so you can use special response classes
{"persons":[{id":"1","name":"name", ...} ] }
public class Response {
#SerializedName("persons")
#Expose
public ArrayList<Person> persones;
}
public class Person {
#SerializedName("id")
#Expose
public String id;
#SerializedName("name")
#Expose
public String name;
#SerializedName("fields_json")
#Expose
public FieldsJson fieldsJson;
}
public class FieldsJson {
#SerializedName("f1")
#Expose
public String f1;
#SerializedName("f2")
#Expose
public String f2;
#SerializedName("f3")
#Expose
public String f3;
}
and set this class as response
#POST("/url")
Call<Response> getPersones(
....
);
So, if you wish You can describe public fieldsJson as Map
Map<String, Object> fieldsJson
It cannot be added as a Map object.
Instead you need to create another java class that contains the f1-f3 variables.
Then add this new class as a variable with the name of fields_json to your current class.
My JSON:
{
"name": "asdf",
"age": "15",
"address": {
"street": "asdf"
}
}
If street is null, with JsonSerialize.Inclusion.NON_NULL, I can get..
{
"name": "asdf",
"age": "15",
"address": {}
}
But I want to get something like this.. (when address is not null, it is a new/empty object. But street is null.)
{
"name": "asdf",
"age": "15"
}
I thought to have custom serialization feature like JsonSerialize.Inclusion.VALID_OBJECT.
Adding isValid() method in the Address class then if that returns true serialize else don't serialize.
But I don't know how to proceed further/which class to override. Is this possible or any other views on this? Please suggest.
Added classes
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
Customer customer = new Customer();
customer.setName("name");
customer.setAddress(new Address());
mapper.writeValue(new File("d:\\customer.json"), customer);
}
#JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class Customer {
private String name;
private Address address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
#JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class Address {
private String street;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
Note: I am not worrying about deserialization now. i.e, loss of address object.
Thanks in advance.
Customized JSON Object using Serialization is Very Simple.
I have wrote a claas in my project i am giving u a clue that how to Implement this in Projects
Loan Application (POJO Class)
import java.io.Serializable;
import java.util.List;
import org.webservice.business.serializer.LoanApplicationSerializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
#JsonSerialize(using=LoanApplicationSerializer.class)
public class LoanApplication implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private double amount;
private User borrowerId;
private String businessType;
private String currency;
private int duration;
private Date lastChangeDate;
private long loanApplicationId;
private String myStory;
private String productCategory;
private String purpose;
private Date startDate;
private String status;
private String type;
private String salesRepresentative;
Now LoanApplicationSerializer class that contains the Customization using Serialization Logic................
package org.ovamba.business.serializer;
import java.io.IOException;
import org.webservice.business.dto.LoanApplication;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
public class LoanApplicationSerializer extends JsonSerializer<LoanApplication> {
#Override
public void serialize(LoanApplication prm_objObjectToSerialize, JsonGenerator prm_objJsonGenerator, SerializerProvider prm_objSerializerProvider) throws IOException, JsonProcessingException {
if (null == prm_objObjectToSerialize) {
} else {
try {
prm_objJsonGenerator.writeStartObject();
prm_objJsonGenerator.writeNumberField("applicationId", prm_objObjectToSerialize.getLoanApplicationId());
prm_objJsonGenerator.writeStringField("status", prm_objObjectToSerialize.getStatus());
prm_objJsonGenerator.writeNumberField("amount", prm_objObjectToSerialize.getAmount());
prm_objJsonGenerator.writeNumberField("startdate", prm_objObjectToSerialize.getStartDate().getTime());
prm_objJsonGenerator.writeNumberField("duration", prm_objObjectToSerialize.getDuration());
prm_objJsonGenerator.writeStringField("businesstype", prm_objObjectToSerialize.getBusinessType());
prm_objJsonGenerator.writeStringField("currency", prm_objObjectToSerialize.getCurrency());
prm_objJsonGenerator.writeStringField("productcategory", prm_objObjectToSerialize.getProductCategory());
prm_objJsonGenerator.writeStringField("purpose", prm_objObjectToSerialize.getPurpose());
prm_objJsonGenerator.writeStringField("mystory", prm_objObjectToSerialize.getMyStory());
prm_objJsonGenerator.writeStringField("salesRepresentative", prm_objObjectToSerialize.getSalesRepresentative());
} catch (Exception v_exException) {
//ExceptionController.getInstance().error("Error while Serializing the Loan Application Object", v_exException);
} finally {
prm_objJsonGenerator.writeEndObject();
}
}
}
}
Hope This may help u alot. Thanks..
You can do it by annotating your class with #JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
Example:
#JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public myClass{
// attributes and accessors
}
You can find some useful informations at Jackson faster xml