How to Convert Pojo to JSON and Vice Versa

Convert Java Object to Json and Vice versa?

I got my Answer but Thank you for Your Responses.

 Map<Long,List<Person>> map=new HashMap<Long,List<Person>>();
//adding some data

Gson gson=new Gson();
String mapJsonStr=gson.toJson(map);

//mapJsonStr : is my map's JSon Strin

for reverse

 TypeToken<Map<Long,List<Person>>> token = new TypeToken<Map<Long,List<Person>>>(){};
Map<Long,List<Person>> map_new=new HashMap<Long,List<Person>>();
map_new=gson.fromJson(mapJsonStr,token.getType());

//origian map
// map_new is my Map get from map's Json String

That's It.Thank you

How to convert POJO to Map and vice versa in Java?

My use case is to convert any arbitrary POJO to Map and back from Map to POJO.

You could use Jackson, a popular JSON parser for Java:

ObjectMapper mapper = new ObjectMapper();

// Convert POJO to Map
Map<String, Object> map =
mapper.convertValue(foo, new TypeReference<Map<String, Object>>() {});

// Convert Map to POJO
Foo anotherFoo = mapper.convertValue(map, Foo.class);

According to the Jackson documentation, this method is functionally similar to first serializing given value into JSON, and then binding JSON data into value of given type, but should be more efficient since full serialization does not (need to) occur. However, same converters (serializers and deserializers) will be used as for data binding, meaning same object mapper configuration works.

Create JSONObject from POJO

Simply use the java Gson API:

Gson gson = new GsonBuilder().create();
String json = gson.toJson(obj);// obj is your object

And then you can create a JSONObject from this json String, like this:

JSONObject jsonObj = new JSONObject(json);

Take a look at Gson user guide and this SIMPLE GSON EXAMPLE for more information.

How do I convert this JSON file from POJO and vice versa

I used the following online tool to convert your JSON to POJOs; http://www.jsonschema2pojo.org/

Below is the generated JSON (with Gson annotations). First is the D class.

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class D {

@SerializedName("ComplaintNo")
@Expose
private String complaintNo;
@SerializedName("Status")
@Expose
private String status;
@SerializedName("UpdateDate")
@Expose
private String updateDate;
@SerializedName("UpdateTime")
@Expose
private String updateTime;
@SerializedName("ComplaintReason")
@Expose
private String complaintReason;
@SerializedName("ClosureType")
@Expose
private String closureType;
@SerializedName("Ibase")
@Expose
private String ibase;
@SerializedName("Component")
@Expose
private String component;
@SerializedName("ProductId")
@Expose
private String productId;
@SerializedName("ProductDescription")
@Expose
private String productDescription;
@SerializedName("Identification")
@Expose
private String identification;
@SerializedName("Cat1")
@Expose
private String cat1;
@SerializedName("Cat2")
@Expose
private String cat2;
@SerializedName("Cat3")
@Expose
private String cat3;
@SerializedName("StatusReason")
@Expose
private String statusReason;
@SerializedName("VisitDate")
@Expose
private String visitDate;
@SerializedName("VisitTime")
@Expose
private String visitTime;
@SerializedName("NoOfVisit")
@Expose
private String noOfVisit;
@SerializedName("SerialNo")
@Expose
private String serialNo;
@SerializedName("OtherSpecify")
@Expose
private String otherSpecify;
@SerializedName("Complaint_product")
@Expose
private List<ComplaintProduct> complaintProduct = null;
@SerializedName("Complaint_retuarn")
@Expose
private List<ComplaintRetuarn> complaintRetuarn = null;

public String getComplaintNo() {
return complaintNo;
}

public void setComplaintNo(String complaintNo) {
this.complaintNo = complaintNo;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getUpdateDate() {
return updateDate;
}

public void setUpdateDate(String updateDate) {
this.updateDate = updateDate;
}

public String getUpdateTime() {
return updateTime;
}

public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}

public String getComplaintReason() {
return complaintReason;
}

public void setComplaintReason(String complaintReason) {
this.complaintReason = complaintReason;
}

public String getClosureType() {
return closureType;
}

public void setClosureType(String closureType) {
this.closureType = closureType;
}

public String getIbase() {
return ibase;
}

public void setIbase(String ibase) {
this.ibase = ibase;
}

public String getComponent() {
return component;
}

public void setComponent(String component) {
this.component = component;
}

public String getProductId() {
return productId;
}

public void setProductId(String productId) {
this.productId = productId;
}

public String getProductDescription() {
return productDescription;
}

public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}

public String getIdentification() {
return identification;
}

public void setIdentification(String identification) {
this.identification = identification;
}

public String getCat1() {
return cat1;
}

public void setCat1(String cat1) {
this.cat1 = cat1;
}

public String getCat2() {
return cat2;
}

public void setCat2(String cat2) {
this.cat2 = cat2;
}

public String getCat3() {
return cat3;
}

public void setCat3(String cat3) {
this.cat3 = cat3;
}

public String getStatusReason() {
return statusReason;
}

public void setStatusReason(String statusReason) {
this.statusReason = statusReason;
}

public String getVisitDate() {
return visitDate;
}

public void setVisitDate(String visitDate) {
this.visitDate = visitDate;
}

public String getVisitTime() {
return visitTime;
}

public void setVisitTime(String visitTime) {
this.visitTime = visitTime;
}

public String getNoOfVisit() {
return noOfVisit;
}

public void setNoOfVisit(String noOfVisit) {
this.noOfVisit = noOfVisit;
}

public String getSerialNo() {
return serialNo;
}

public void setSerialNo(String serialNo) {
this.serialNo = serialNo;
}

public String getOtherSpecify() {
return otherSpecify;
}

public void setOtherSpecify(String otherSpecify) {
this.otherSpecify = otherSpecify;
}

public List<ComplaintProduct> getComplaintProduct() {
return complaintProduct;
}

public void setComplaintProduct(List<ComplaintProduct> complaintProduct) {
this.complaintProduct = complaintProduct;
}

public List<ComplaintRetuarn> getComplaintRetuarn() {
return complaintRetuarn;
}

public void setComplaintRetuarn(List<ComplaintRetuarn> complaintRetuarn) {
this.complaintRetuarn = complaintRetuarn;
}

}

Then the ComplaintProduct.

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class ComplaintProduct {

@SerializedName("SequenceNo")
@Expose
private String sequenceNo;
@SerializedName("SparepartId")
@Expose
private String sparepartId;
@SerializedName("Quantity")
@Expose
private String quantity;
@SerializedName("Group")
@Expose
private String group;
@SerializedName("Model")
@Expose
private String model;

public String getSequenceNo() {
return sequenceNo;
}

public void setSequenceNo(String sequenceNo) {
this.sequenceNo = sequenceNo;
}

public String getSparepartId() {
return sparepartId;
}

public void setSparepartId(String sparepartId) {
this.sparepartId = sparepartId;
}

public String getQuantity() {
return quantity;
}

public void setQuantity(String quantity) {
this.quantity = quantity;
}

public String getGroup() {
return group;
}

public void setGroup(String group) {
this.group = group;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

}

Finally the ComplaintRetuarn (please correct the spelling here).

package com.example;

public class ComplaintRetuarn {

}

You can use Gson to convert back and forth between JSON and your POJO. First ensure you have included Gson in your project.

dependencies {
implementation 'com.google.code.gson:gson:2.8.5'
}

To convert to a POJO from JSON use;

D d = new Gson().fromJson(json, D.class);

Then back to JSON;

String json = gson.toJson(d);


Related Topics



Leave a reply



Submit