[英]Salesforce Java rest api json structure for object creation
[英]json to table structure api in Java
我已经在互联网上搜索了一段时间,以获得将json转换为表格格式的API。 我没有尝试过的任何代码。 如果您有任何想法,请直接给我。
例如:杰森
{ “名”: “rinu”, “年龄”: “14”, “手机”:[{ “COUNTRYCODE”:91, “数字”: “99862656”},{ “COUNTRYCODE”:91, “数字”:” 675432 “}],” OtherDetails “:[{” 活动“:真}]}
输出可以是(任何分隔)
rinu|14|91|99862656|true
rinu|14|91|675432|true
我不需要任何现成的东西,如果我得到类似的东西,我可以重写它。
您可能需要这样:
JacksonRead.java
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.codehaus.jackson.map.ObjectMapper;
public class JacksonRead {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
try {
Example example = mapper.readValue(new File("d:\\user.json"),
Example.class);
StringBuilder builder = new StringBuilder();
int i = 0;
for (Phone phone : example.getPhone()) {
builder.append(example.getName()).append("|");
builder.append(example.getAge()).append("|");
builder.append(phone.getCountryCode()).append("|")
.append(phone.getNumber()).append("|")
.append(example.getOtherDetails().get(i).getActive())
.append("|");
builder.append("\n");
}
File file = new File("d:\\user.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(builder.toString());
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Example.java
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.annotate.JsonProperty;
public class Example {
@JsonProperty("name")
private String name;
@JsonProperty("age")
private String age;
@JsonProperty("Phone")
private List<Phone> Phone = new ArrayList<Phone>();
@JsonProperty("OtherDetails")
private List<OtherDetail> OtherDetails = new ArrayList<OtherDetail>();
/**
*
* @return The name
*/
@JsonProperty("name")
public String getName() {
return name;
}
/**
*
* @param name
* The name
*/
@JsonProperty("name")
public void setName(String name) {
this.name = name;
}
/**
*
* @return The age
*/
@JsonProperty("age")
public String getAge() {
return age;
}
/**
*
* @param age
* The age
*/
@JsonProperty("age")
public void setAge(String age) {
this.age = age;
}
/**
*
* @return The Phone
*/
@JsonProperty("Phone")
public List<Phone> getPhone() {
return Phone;
}
/**
*
* @param Phone
* The Phone
*/
@JsonProperty("Phone")
public void setPhone(List<Phone> Phone) {
this.Phone = Phone;
}
/**
*
* @return The OtherDetails
*/
@JsonProperty("OtherDetails")
public List<OtherDetail> getOtherDetails() {
return OtherDetails;
}
/**
*
* @param OtherDetails
* The OtherDetails
*/
@JsonProperty("OtherDetails")
public void setOtherDetails(List<OtherDetail> OtherDetails) {
this.OtherDetails = OtherDetails;
}
@Override
public String toString() {
return "Example [name=" + name + ", age=" + age + ", Phone=" + Phone
+ ", OtherDetails=" + OtherDetails + "]";
}
}
Phone.java
import org.codehaus.jackson.annotate.JsonProperty;
public class Phone {
@JsonProperty("countryCode")
private Integer countryCode;
@JsonProperty("number")
private String number;
/**
*
* @return The countryCode
*/
@JsonProperty("countryCode")
public Integer getCountryCode() {
return countryCode;
}
/**
*
* @param countryCode
* The countryCode
*/
@JsonProperty("countryCode")
public void setCountryCode(Integer countryCode) {
this.countryCode = countryCode;
}
/**
*
* @return The number
*/
@JsonProperty("number")
public String getNumber() {
return number;
}
/**
*
* @param number
* The number
*/
@JsonProperty("number")
public void setNumber(String number) {
this.number = number;
}
@Override
public String toString() {
return "Phone [countryCode=" + countryCode + ", number=" + number + "]";
}
}
OtherDetail.java
import org.codehaus.jackson.annotate.JsonProperty;
public class OtherDetail {
@JsonProperty("Active")
private Boolean Active;
/**
*
* @return The Active
*/
@JsonProperty("Active")
public Boolean getActive() {
return Active;
}
/**
*
* @param Active
* The Active
*/
@JsonProperty("Active")
public void setActive(Boolean Active) {
this.Active = Active;
}
@Override
public String toString() {
return "OtherDetail [Active=" + Active + "]";
}
}
user.json
{"name":"rinu","age":"14","Phone":[{"countryCode":91,"number":"99862656"},{"countryCode":91,"number":"675432"}],"OtherDetails":[{"Active":true}]}
我尝试了带有json的库json2flat
{"name":"rinu","age":"14","Phone":[{"countryCode":91,"number":"99862656"},{"countryCode":91,"number":"675432"}],"OtherDetails":[{"Active":true}]}
它给出了CSV像::
rinu|14|91|99862656| rinu|14|91|675432 | rinu| | | |true
但是,如果您将json调整为::
{"name":"rinu","age":"14","Phone":[{"countryCode":91,"number":"99862656","Active":true},{"countryCode":91,"number":"675432","Active":true}]}
它完全按照您的要求提供了csv。
rinu|14|91|99862656|true rinu|14|91|675432|true
试试看。 毕竟,解决方案取决于用户想要如何解释json。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.