这是使用 Jackson Databind 读取 JSON 文本并使用 Apache Commons CSV 将其写入 CSV 的代码。
使用 Databind 时,您需要 Java POJO 类,可以选择使用 @JsonProperty 进行注释以指定 JSON 字段名称。
您的 JSON 文本内嵌在底部以生成 MCVE。
import java.util.List;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
public static void main(String[] args) throws Exception {
Root root = new ObjectMapper().readValue(Input.json, Root.class);
CSVPrinter printer = CSVFormat.DEFAULT
.withHeader("Name", "Author", "Address", "Service", "Link")
.print(System.out);
for (Site site : root.getSites())
for (Service service : site.getServices())
printer.printRecord(site.getName(), site.getAuthor(), site.getAddress(),
service.getService(), service.getLink());
}
}
class Root {
private List<Site> sites;
@JsonProperty("Yjson")
public List<Site> getSites() {
return this.sites;
}
public void setSites(List<Site> sites) {
this.sites = sites;
}
}
class Site {
private String name;
private String author;
private String address;
private List<Service> services;
@JsonProperty("Name")
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@JsonProperty("Author")
public String getAuthor() {
return this.author;
}
public void setAuthor(String author) {
this.author = author;
}
@JsonProperty("Address")
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
@JsonProperty("Company Services")
public List<Service> getServices() {
return this.services;
}
public void setServices(List<Service> services) {
this.services = services;
}
}
class Service {
private String service;
private String link;
@JsonProperty("Service")
public String getService() {
return this.service;
}
public void setService(String service) {
this.service = service;
}
@JsonProperty("Link")
public String getLink() {
return this.link;
}
public void setLink(String link) {
this.link = link;
}
}
class Input {
static final String json =
"{\"Yjson\":\n" +
"[\n" +
"{\n" +
"\"Name\": \"crunchify.com\",\n" +
" \"Author\": \"App Shah\",\n" +
" \"Address\": \"New York\",\n" +
" \"Company Services\": [{\n" +
" \"Service\": \"Site SEO Review\",\n" +
" \"Link\": \"https://crunchify.com/services/site-seo-review-service/\"\n" +
" }, {\n" +
" \"Service\": \"Full Website Design Service\",\n" +
" \"Link\": \"https://crunchify.com/services/full-website-design-service/\"\n" +
" }, {\n" +
" \"Service\": \"WordPress Optimization & Consultation\",\n" +
" \"Link\": \"https://crunchify.com/services/wordpress-optimization-service/\"\n" +
" }, {\n" +
" \"Service\": \"WordPress Optimization & Consultation\",\n" +
" \"Link\": \"https://crunchify.com/services/wordpress-optimization-service/\"\n" +
" }]\n" +
"},\n" +
"{\n" +
" \"Name\": \"xyz.com\",\n" +
" \"Author\": \"xyz Shah\",\n" +
" \"Address\": \"toronto\",\n" +
" \"Company Services\": [{\n" +
" \"Service\": \"Site SEO Review\",\n" +
" \"Link\": \"https://crunchify.com/services/site-seo-review-service/\"\n" +
" }, {\n" +
" \"Service\": \"Full Website Design Service\",\n" +
" \"Link\": \"https://crunchify.com/services/full-website-design-service/\"\n" +
" }, {\n" +
" \"Service\": \"WordPress Optimization & Consultation\",\n" +
" \"Link\": \"https://crunchify.com/services/wordpress-optimization-service/\"\n" +
" }]\n" +
"}\n" +
"]\n" +
"}";
}
输出
Name,Author,Address,Service,Link
crunchify.com,App Shah,New York,Site SEO Review,https://crunchify.com/services/site-seo-review-service/
crunchify.com,App Shah,New York,Full Website Design Service,https://crunchify.com/services/full-website-design-service/
crunchify.com,App Shah,New York,WordPress Optimization & Consultation,https://crunchify.com/services/wordpress-optimization-service/
crunchify.com,App Shah,New York,WordPress Optimization & Consultation,https://crunchify.com/services/wordpress-optimization-service/
xyz.com,xyz Shah,toronto,Site SEO Review,https://crunchify.com/services/site-seo-review-service/
xyz.com,xyz Shah,toronto,Full Website Design Service,https://crunchify.com/services/full-website-design-service/
xyz.com,xyz Shah,toronto,WordPress Optimization & Consultation,https://crunchify.com/services/wordpress-optimization-service/
如果您使用 Maven,则需要以下两个依赖项:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.4</version>
</dependency>