Java中json到表结构api
我在互联网上搜索了一段时间,以获得一个将json转换为表格格式的API。我没有任何我尝试过的代码。如果你对此有任何想法,请直接告诉我 例如:JsonJava中json到表结构api,java,json,jackson,Java,Json,Jackson,我在互联网上搜索了一段时间,以获得一个将json转换为表格格式的API。我没有任何我尝试过的代码。如果你对此有任何想法,请直接告诉我 例如:Json {"name":"rinu","age":"14","Phone":[{"countryCode":91,"number":"99862656"},{"countryCode":91,"number":"675432"}],"OtherDetails":[{"Active":true}]} {“姓名”:“rinu”,“年龄”:“14”,“电话”:
{"name":"rinu","age":"14","Phone":[{"countryCode":91,"number":"99862656"},{"countryCode":91,"number":"675432"}],"OtherDetails":[{"Active":true}]}
{“姓名”:“rinu”,“年龄”:“14”,“电话”:[{“国家代码”:91,“号码”:“99862656”},{“国家代码”:91,“号码”:“675432”}],“其他详细信息”:[{“现行”:真}]}
输出可以是(任何分离的)
我不想要任何现成的东西,如果我得到类似的东西,我可以重新编写。您可能需要这个:
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();
}
}
}
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 + "]";
}
}
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 + "]";
}
}
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 + "]";
}
}
Example.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();
}
}
}
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 + "]";
}
}
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 + "]";
}
}
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 + "]";
}
}
OtherDetail.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();
}
}
}
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 + "]";
}
}
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 + "]";
}
}
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的库
{"name":"rinu","age":"14","Phone":[{"countryCode":91,"number":"99862656"},{"countryCode":91,"number":"675432"}],"OtherDetails":[{"Active":true}]}
{“姓名”:“rinu”,“年龄”:“14”,“电话”:[{“国家代码”:91,“号码”:“99862656”},{“国家代码”:91,“号码”:“675432”}],“其他详细信息”:[{“现行”:真}]}
它给出一个CSV,如::
rinu|14|91|99862656|
rinu|14|91|675432 |
rinu| | | |true
里努| 14 | 91 | 99862656|
里努| 14 | 91 | 675432|
里努| | | | |真
但如果您将json稍微调整如下:
{"name":"rinu","age":"14","Phone":[{"countryCode":91,"number":"99862656","Active":true},{"countryCode":91,"number":"675432","Active":true}]}
{“姓名”:“rinu”,“年龄”:“14”,“电话”:[{“国家代码”:91,“号码”:“99862656”,“活动”:true},{“国家代码”:91,“号码”:“675432”,“活动”:true}]}
它提供的csv完全符合您的要求
rinu|14|91|99862656|true
rinu|14|91|675432|true
里努| 14 | 91 | 99862656 |真
里努| 14 | 91 | 675432 |真
试试看。毕竟,解决方案取决于用户想要如何解释json