Reputation: 713
I am trying to create this json format so I can test this request with different values but the problem is creating this json format is difficult for me in java.
As you can see I am using java and Using the class JSONObject and then assigning one one key and its value to create this format. But looks like this is a wrong approach.
{
"to": "918892******",
"type": "contacts",
"contacts": [{
"addresses": [{
"city": "bangalore",
"country": "India",
"country_code": "91",
"state": "----",
"street": "** Main",
"type": "Home",
"zip": "000000"
}],
"birthday": "08-09-1994",
"emails": [{
"email": "*******[email protected]",
"type": "Email"
}],
"ims": [],
"name": {
"first_name": "Sobhit",
"formatted_name": "l",
"last_name": "Sharma"
},
"org": {
"company": "------"
},
"phones": [{
"phone": "890464----",
"type": "Work"
}],
"urls": []
}],
"callback": "{{callback}}"
}
My code to create is
public void ContactJson(){
JSONObject jsonObj = new JSONObject();
JSONObject pairKey = new JSONObject();
JSONArray arrayMain = new JSONArray();
JSONObject Array_itemMain = new JSONObject();
pairKey.put("first_name","first_name");
pairKey.put("formatted_name","formatted_name");
pairKey.put("last_name","last_name");
jsonObj.put("to", "To");
jsonObj.put("type","type");
jsonObj.put("contacts", arrayMain,pairKey);
Array_itemMain.put("addresses","addresses");
Array_itemMain.put("birthday","08-09-1994");
Array_itemMain.put("emails","*****@gmail.com");
Array_itemMain.put("ims","null");
Array_itemMain.put("name","sobhit");
Array_itemMain.put("org","addresses");
Array_itemMain.put("Phones","*****");
Array_itemMain.put("urls","url");
jsonObj.put("callback","Calbback");
arrayMain.add(Array_itemMain);
CreatedJson = jsonObj.toString();
System.out.println(CreatedJson);
}
The problem start when I am creating multiple json array and assgining to main array where it shows error because its taking only one array in the main json body. Not sure how to achieve this but any help would be really appritiacted.
Upvotes: 0
Views: 4671
Reputation: 713
I have created desired output json format by doing below.
JSONObject jsonObj = new JSONObject();
JSONObject jsonObjName = new JSONObject();
JSONObject jsonObjOrg = new JSONObject();
JSONArray arrayContacts = new JSONArray();
JSONObject Array_itemContacts = new JSONObject();
JSONArray arrayEmails = new JSONArray();
JSONObject Array_itemEmails = new JSONObject();
JSONArray arrayAddress = new JSONArray();
JSONObject array_itemAdrress = new JSONObject();
JSONArray arrayPhones = new JSONArray();
JSONObject Array_itemPhones = new JSONObject();
jsonObj.put("to",to);
jsonObj.put("type","contacts");
jsonObj.put("contacts",arrayContacts);
Array_itemContacts.put("addresses",arrayAddress);
Array_itemContacts.put("emails",arrayEmails);
Array_itemContacts.put("phones",arrayPhones);
Array_itemContacts.put("birthday",DOB);
Array_itemContacts.put("name",jsonObjName);
Array_itemContacts.put("org",jsonObjOrg);
Array_itemPhones.put("phone",Contact_Off);
Array_itemPhones.put("type",ContactType);
arrayPhones.add(Array_itemPhones);
arrayContacts.add(Array_itemContacts);
array_itemAdrress.put("city",city);
array_itemAdrress.put("country",country);
array_itemAdrress.put("country_code",country_code);
array_itemAdrress.put("state",state);
array_itemAdrress.put("street",street);
array_itemAdrress.put("type",addressType);
array_itemAdrress.put("zip",Zip);
arrayAddress.add(array_itemAdrress);
Array_itemEmails.put("email",emailId);
Array_itemEmails.put("type",EmailType);
arrayEmails.add(Array_itemEmails);
jsonObj.put("callback",callback);
jsonObjName.put("first_name",FirstName);
jsonObjName.put("formatted_name",ContactDisplayName);
jsonObjName.put("last_name",LastName);
jsonObjOrg.put("company",Company);
It was difficult and time taking but yes this what I was looking for. Also I appreciate guys who helped me here on stack over flow.
Upvotes: 1
Reputation: 15253
You may generate POJO classes in the structure that you need, then instantiate an object using the classes generated with the required values and then use the object in your request.
You may use something like this to generate POJOs from the json structure.
Example:
public class Test {
private String to;
private String type;
ArrayList < Object > contacts = new ArrayList < Object > ();
private String callback;
// Getter Methods
public String getTo() {
return to;
}
public String getType() {
return type;
}
public String getCallback() {
return callback;
}
// Setter Methods
public void setTo(String to) {
this.to = to;
}
public void setType(String type) {
this.type = type;
}
public void setCallback(String callback) {
this.callback = callback;
}
}
Once you've used the above to create the classes, instantiate like below :
Test request = new Test();
request.setTo("TestString");
request.setType("TestSting");
I have only given minor examples for brevity sake. I hope you get the idea.
In case you don't want to manually set the values to all the fields, you may even use Jackson's ObjectMapper
to map the json as a string to the POJO class that you create.
Upvotes: 2
Reputation: 23674
I would start by making a class to represent the data. This class can be used as a "Schema" of sorts. Defining how the JSON is structured. There are actually a lot of tools that can automate this process. See http://www.jsonschema2pojo.org/
Example output:
public class Contact {
private List<Address> addresses = null;
private String birthday;
private List<Email> emails = null;
private List<Object> ims = null;
private Name name;
private Org org;
private List<Phone> phones = null;
private List<String> urls = null;
// getters and setters
...
}
public class Address {
private String city;
private String country;
private String countryCode;
private String state;
private String street;
private String type;
private String zip;
// getters and setters
...
}
// other classes like Email and Phone
...
Using a library like jackson you can convert back and forth from json to java object quite easily:
// String to Class
String jsonString = ...;
ObjectMapper mapper = new ObjectMapper();
MyCustomPojo pojo = mapper.readValue(jsonString, MyCustomPojo.class);
// Class to String
MyCustomPojo pojo = ...;
ObjectMapper mapper = new ObjectMapper();
String jsonString mapper.writeValueAsString(pojo);
Upvotes: 0
Reputation: 784
Create Pojo Class as following
-Address.java
package example;
import java.io.Serializable;
public class Address implements Serializable
{
private String city;
private String country;
private String country_code;
private String state;
private String street;
private String type;
private String zip;
private final static long serialVersionUID = -561722446550829275L;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCountry_code() {
return country_code;
}
public void setCountry_code(String country_code) {
this.country_code = country_code;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
}
-Contact.java
package example;
import java.io.Serializable;
import java.util.List;
public class Contact implements Serializable
{
private List<Address> addresses = null;
private String birthday;
private List<Email> emails = null;
private List<Object> ims = null;
private Name name;
private Org org;
private List<Phone> phones = null;
private List<Object> urls = null;
private final static long serialVersionUID = -6494079357709578167L;
public List<Address> getAddresses() {
return addresses;
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public List<Email> getEmails() {
return emails;
}
public void setEmails(List<Email> emails) {
this.emails = emails;
}
public List<Object> getIms() {
return ims;
}
public void setIms(List<Object> ims) {
this.ims = ims;
}
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
public Org getOrg() {
return org;
}
public void setOrg(Org org) {
this.org = org;
}
public List<Phone> getPhones() {
return phones;
}
public void setPhones(List<Phone> phones) {
this.phones = phones;
}
public List<Object> getUrls() {
return urls;
}
public void setUrls(List<Object> urls) {
this.urls = urls;
}
}
-Email.java
package example;
import java.io.Serializable;
public class Email implements Serializable
{
private String email;
private String type;
private final static long serialVersionUID = 465076650464061474L;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
-Name.java
package example;
import java.io.Serializable;
public class Name implements Serializable
{
private String first_name;
private String formatted_name;
private String last_name;
private final static long serialVersionUID = -823937810389965985L;
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getFormatted_name() {
return formatted_name;
}
public void setFormatted_name(String formatted_name) {
this.formatted_name = formatted_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
}
-Org.java
package example;
import java.io.Serializable;
public class Org implements Serializable
{
private String company;
private final static long serialVersionUID = -4354792147573896615L;
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
}
-Phone.java
package example;
import java.io.Serializable;
public class Phone implements Serializable
{
private String phone;
private String type;
private final static long serialVersionUID = 5985121027482757572L;
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
-Request.java
package example;
import java.io.Serializable;
import java.util.List;
public class Request implements Serializable
{
private String to;
private String type;
private List<Contact> contacts = null;
private String callback;
private final static long serialVersionUID = -7588225121633534682L;
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<Contact> getContacts() {
return contacts;
}
public void setContacts(List<Contact> contacts) {
this.contacts = contacts;
}
public String getCallback() {
return callback;
}
public void setCallback(String callback) {
this.callback = callback;
}
}
Then after this, you can use Gson to convert the Request
object to JSON.
Request request=new Request();
//set values to all variables and sub objects.
Gson gson = new Gson();
String outputJson = gson.toJson(request);
System.out.println(outputJson);
Hope this helps.
Upvotes: 0