1

Hi i want to convert this json to json object in java so that i can pass it to http request to call an api

{ "aliasNaming": true, "dataServiceType": "BROWSE", "deviceName": "MyDevice", "langPref": " ", "maxPageSize": "2000", "outputType": "VERSION1", "password": "!jshjhsdhshdj", "query": { "autoClear": true, "autoFind": true, "condition": [ { "controlId": "F4211.CO", "operator": "EQUAL", "value": [ { "content": "00098", "specialValueId": "LITERAL" } ] }, { "controlId": "F4211.DCTO", "operator": "EQUAL", "value": [ { "content": "SM", "specialValueId": "LITERAL" } ] }, { "controlId": "F4211.UPMJ", "operator": "GREATER_EQUAL", "value": [ { "content": "01/01/17", "specialValueId": "LITERAL" } ] } ], "matchType": "MATCH_ALL" }, "returnControlIDs": "F4211.DOCO|F4211.TRDJ|F4211.CRCD|F4211.AN8|F4211.DSC2|F4211.DSC1|F4211.LITM|F4211.LOTN|F4211.UORG|F4211.UPRC|F4211.AEXP", "targetName": "F4211", "targetType": "table", "token": "044biPNadxNVGhyAKdrImoniK98OOa2l86ZA63qCr4gE5o=MDIwMDA4LTIyNDU5MjUxMTY2MzY3NTA3MTRNeURldmljZTE1Mzc0MjYwMjAyNTk=", "username": "Ali" }

i have created 4 models using http://www.jsonschema2pojo.org. those models just have getter setter in it. look something like this

 @JsonProperty("aliasNaming")
private Boolean aliasNaming;
@JsonProperty("dataServiceType")
private String dataServiceType;
@JsonProperty("deviceName")
private String deviceName;
@JsonProperty("langPref")
private String langPref;
@JsonProperty("maxPageSize")
private String maxPageSize;
@JsonProperty("outputType")
private String outputType;
@JsonProperty("password")
private String password;
@JsonProperty("query")
private Query query;
@JsonProperty("returnControlIDs")
private String returnControlIDs;
@JsonProperty("targetName")
private String targetName;
@JsonProperty("targetType")
private String targetType;
@JsonProperty("token")
private String token;
@JsonProperty("username")
private String username;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("aliasNaming")
public Boolean getAliasNaming() {
    return aliasNaming;
}

@JsonProperty("aliasNaming")
public void setAliasNaming(Boolean aliasNaming) {
    this.aliasNaming = aliasNaming;
}

@JsonProperty("dataServiceType")
public String getDataServiceType() {
    return dataServiceType;
}

@JsonProperty("dataServiceType")
public void setDataServiceType(String dataServiceType) {
    this.dataServiceType = dataServiceType;
}

@JsonProperty("deviceName")
public String getDeviceName() {
    return deviceName;
}

@JsonProperty("deviceName")
public void setDeviceName(String deviceName) {
    this.deviceName = deviceName;
}

@JsonProperty("langPref")
public String getLangPref() {
    return langPref;
}

@JsonProperty("langPref")
public void setLangPref(String langPref) {
    this.langPref = langPref;
}

@JsonProperty("maxPageSize")
public String getMaxPageSize() {
    return maxPageSize;
}

@JsonProperty("maxPageSize")
public void setMaxPageSize(String maxPageSize) {
    this.maxPageSize = maxPageSize;
}

@JsonProperty("outputType")
public String getOutputType() {
    return outputType;
}

@JsonProperty("outputType")
public void setOutputType(String outputType) {
    this.outputType = outputType;
}

@JsonProperty("password")
public String getPassword() {
    return password;
}

@JsonProperty("password")
public void setPassword(String password) {
    this.password = password;
}

@JsonProperty("query")
public Query getQuery() {
    return query;
}

@JsonProperty("query")
public void setQuery(Query query) {
    this.query = query;
}

@JsonProperty("returnControlIDs")
public String getReturnControlIDs() {
    return returnControlIDs;
}

@JsonProperty("returnControlIDs")
public void setReturnControlIDs(String returnControlIDs) {
    this.returnControlIDs = returnControlIDs;
}

@JsonProperty("targetName")
public String getTargetName() {
    return targetName;
}

@JsonProperty("targetName")
public void setTargetName(String targetName) {
    this.targetName = targetName;
}

@JsonProperty("targetType")
public String getTargetType() {
    return targetType;
}

@JsonProperty("targetType")
public void setTargetType(String targetType) {
    this.targetType = targetType;
}

@JsonProperty("token")
public String getToken() {
    return token;
}

@JsonProperty("token")
public void setToken(String token) {
    this.token = token;
}

@JsonProperty("username")
public String getUsername() {
    return username;
}

@JsonProperty("username")
public void setUsername(String username) {
    this.username = username;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
}

Now i want to set the values in these models by creating their respective objects and finally i got one main object with all the data. like this

  Value Vobj1 = new Value();
  Vobj1.setContent("00098");
  Vobj1.setSpecialValueId("LITERAL");


  List<Value> valueList1= new ArrayList<Value>();
  valueList1.add(Vobj1);

  Value Vobj2 = new Value();
  Vobj2.setContent("SM");
  Vobj2.setSpecialValueId("LITERAL");

  List<Value> valueList2= new ArrayList<Value>();
  valueList2.add(Vobj2);

  Value Vobj3 = new Value();
  Vobj3.setContent("01/01/17");
  Vobj3.setSpecialValueId("LITERAL");

  List<Value> valueList3= new ArrayList<Value>();
  valueList3.add(Vobj3);

  Condition Cobj1 = new Condition();
  Cobj1.setControlId("F4211.CO");
  Cobj1.setOperator("EQUAL");
  Cobj1.setValue(valueList1);

  Condition Cobj2 = new Condition();
  Cobj2.setControlId("F4211.DCTO");
  Cobj2.setOperator("EQUAL");
  Cobj2.setValue(valueList1);

  Condition Cobj3 = new Condition();
  Cobj3.setControlId("F4211.UPMJ");
  Cobj3.setOperator("GREATER_EQUAL");
  Cobj3.setValue(valueList1);

  List<Condition> conditionList1 = new ArrayList<Condition>();
  conditionList1.add(Cobj1);
  conditionList1.add(Cobj2);
  conditionList1.add(Cobj3);


  Query Qobj1= new Query();
  Qobj1.setAutoClear(true);
  Qobj1.setAutoFind(true);
  Qobj1.setCondition(conditionList1);
  Qobj1.setMatchType("MATCH_ALL");

  JSONStructure obj=new JSONStructure();
  obj.setAliasNaming(true);
  obj.setDataServiceType("BROWSE");
  obj.setDeviceName("MyDevice");
  obj.setLangPref("  ");
  obj.setMaxPageSize("2000");
  obj.setOutputType("VERSION1");
  obj.setPassword("!J0g3t6000");
  obj.setQuery(Qobj1);
  obj.setReturnControlIDs("F4211.DOCO|F4211.TRDJ|F4211.CRCD|F4211.AN8|F4211.DSC2|F4211.DSC1|F4211.LITM|F4211.LOTN|F4211.UORG|F4211.UPRC|F4211.AEXP");
  obj.setTargetName("F4211");
  obj.setTargetType("table");
  obj.setToken(Token);
  obj.setUsername("JOGET");

Now obj is my final object that i am going to pass to an http request and call the api and get the data from it. i want to make sure that my json is created correct, how am i suppose to print the all the data inside this object? and am i going correct with this approach?

2 Answers 2

2

if you use maven put gson into your pom.xml

<dependency>
   <groupId>com.google.code.gson</groupId>
   <artifactId>gson</artifactId>
   <version>2.8.5</version>
</dependency>

then print your object like this

System.out.println(new Gson().toJson(yourObj));

your object will print in json

Sign up to request clarification or add additional context in comments.

1 Comment

thanks. i used this... Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().serializeNulls().create(); LogUtil.info("rizibhai..................",gson.toJson(obj));
0

I found two full working examples that is familiar with your case.

1) Using Gson refer to the tutorial Parse json string and java object into Gson tree model

2) Using Jackson refer to the tutorial Convert Java Object to/from JSON using JACKSON API

Hope this help.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.