如何在不知道键的情况下动态地将嵌套的 Json object/array 转换为基于键的多个列表

How to converting a nested Json object/array to multiple lists based on keys Dynamically without knowing the keys

The below is a sample Json file.

{"Yjson":
[
{
"Name": "crunchify.com",
    "Author": "App Shah",
    "Address": "New York",
    "Company Services": [{
        "Service": "Site SEO Review",
        "Link": "https://crunchify.com/services/site-seo-review-service/"
    }, {
        "Service": "Full Website Design Service",
        "Link": "https://crunchify.com/services/full-website-design-service/"
    }, {
        "Service": "WordPress Optimization & Consultation",
        "Link": "https://crunchify.com/services/wordpress-optimization-service/"
    }, {
        "Service": "WordPress Optimization & Consultation",
        "Link": "https://crunchify.com/services/wordpress-optimization-service/"
    }]
},
{
    "Name": "xyz.com",
    "Author": "xyz Shah",
    "Address": "toronto",
    "Company Services": [{
        "Service": "Site SEO Review",
        "Link": "https://crunchify.com/services/site-seo-review-service/"
    }, {
        "Service": "Full Website Design Service",
        "Link": "https://crunchify.com/services/full-website-design-service/"
    }, {
        "Service": "WordPress Optimization & Consultation",
        "Link": "https://crunchify.com/services/wordpress-optimization-service/"
    }]
}
]
}

How to store all the values of each key in a Arraylist in java ?

例如键名数组列表将包含[crunchicy.com,xyz.com]。同样,每个键都应该有一个数组列表。

示例:

How to parse every json object that can be nested and dynamic in nature and keep them in array lists according to keys ??

这是使用 Jackson Databind and writing it to CSV using Apache Commons CSV.

阅读您的 JSON 文本的代码

使用 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>