[英]How to create the below JSON Object Structure in Java
Below is the json object structure which I want to create. 下面是我要创建的json对象结构。
{
"userInfo": {
"email": "a@a.com",
"full_name": "Niraj Rajbhandari",
"password": "buddhanagar",
"business_category_id": "0",
"user_type": "0"
},
"businessAddress": {
"street1": "Shram Marg",
"state": "Bagmati",
"zip": "0234",
"phone1": "3458364853",
"country": "nepal"
},
"companyInfo": {
"name": "Test",
"vat_date": "2015-05-06",
"vat_status": "0",
"registration_number": "1"
}
}
Below are the my codes. 以下是我的代码。
public class RegistrationModel {
public List<UserInfo> userInfo;
public List<BusinessModel> businessAddress;
public List<CompanyInfoModel> companyInfo;
}
public class Tax
{
Gson gson = new Gson();
RegistrationModel registrationModel = new RegistrationModel();
// User list data
registrationModel.userInfo = new ArrayList<UserInfo>();
UserInfo userInfo = new UserInfo();
userInfo.setEmail(personinfo.get(0).getEmail());
userInfo.setFull_name(personinfo.get(0).getFull_name());
userInfo.setPassword(personinfo.get(0).getPassword());
userInfo.setBusiness_category_id(personinfo.get(0).getBusiness_category_id());
userInfo.setUser_type(personinfo.get(0).getUser_type());
registrationModel.userInfo.add(userInfo);
// Business Address
registrationModel.businessAddress = new ArrayList<BusinessModel>();
BusinessModel businessModel = new BusinessModel();
businessModel.setStreet1(businessModels.get(0).getStreet1());
businessModel.setState(businessModels.get(0).getState());
businessModel.setZip(businessModels.get(0).getZip());
businessModel.setPhone1(businessModels.get(0).getPhone1());
businessModel.setCountry(businessModels.get(0).getCountry());
registrationModel.businessAddress.add(businessModel);
// Company Data
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
//get current date time with Date()
Date date = new Date();
registrationModel.companyInfo = new ArrayList<CompanyInfoModel>();
CompanyInfoModel companyInfoModel = new CompanyInfoModel();
companyInfoModel.setName(businessModels.get(0).getCompany_Name());
companyInfoModel.setVat_date(dateFormat.format(date));
companyInfoModel.setVat_status(0);
companyInfoModel.setRegistration_number(Hmrc_reg_no.getText().toString());
registrationModel.companyInfo.add(companyInfoModel);
gson.toJson(registrationModel)
}
the above code generate the below structure 上面的代码生成下面的结构
{
"userInfo": [{
"email": "a@a.com",
"full_name": "Niraj Rajbhandari",
"password": "buddhanagar",
"business_category_id": "0",
"user_type": "0"
}],
"companyInfo": [{
"name": "Test",
"vat_date": "2015-05-06",
"vat_status": "0",
"registration_number": "1"
}],"businessAddress": [{
"street1": "Shram Marg",
"state": "Bagmati",
"zip": "0234",
"phone1": "3458364853",
"country": "nepal"
}]
}
Can anyone please give me a code to create the json object strucure in JAVA. 谁能给我代码在JAVA中创建json对象结构。 In the above code I don't want []bracket .
在上面的代码中,我不需要[] bracket。 I want the above mention structure
我想要上面提到的结构
use this code: 使用此代码:
JSONObject o1=new JSONObject("userInfo");
o1.put("email","a@a.com");
o1.put(...);
JSONObject o2=new JSONObject("businessAddress");
o2.put("street1","shram Marg");
o2.put(...);
JSONObject o3=new JSONObject("companyInfo");
o3.put("name","Test");
o3.put(...);
JSONArray array=new JSONArray();
array.put(o1);
array.put(o2);
array.put(o3);
change your RegistrationModel to this 将您的RegistrationModel更改为此
public class RegistrationModel {
public UserInfo userInfo;
public BusinessModel businessAddress;
public CompanyInfoModel companyInfo;
}
and then 接着
registrationModel.userInfo = new UserInfo(); //remove this line
.....
your code
.....
registrationModel.userInfo=userInfo;
and same for rest 2 objects also 其余2个对象也一样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.