【问题标题】:How to converting a nested Json object/array to multiple lists based on keys Dynamically without knowing the keys如何在不知道键的情况下动态地将嵌套的 Json 对象/数组转换为基于键的多个列表
【发布时间】:2016-09-11 07:48:04
【问题描述】:

以下是一个示例 Json 文件。

{"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/"
    }]
}
]
}

java中如何将每个键的所有值存储在Arraylist中?

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

示例:

  • 列表名称:[crunchify.com,xyz]

  • 列表作者:[aap shah,xyz shah]

  • 列表名称:[纽约,多伦多]

如何解析每个可以嵌套和动态的json对象 性质并根据键将它们保存在数组列表中??

【问题讨论】:

  • 你有什么尝试吗?
  • 不要将每个字段存储在单独的列表中。 Java 是一种面向对象的语言,您应该使用它。创建一个类,例如Site,并且只有一个列表,例如List<Site>。 Jackson 可以为您直接序列化。
  • @OliverCharlesworth 是的,我尝试使用库 json-flattener 将 json 字符串展平为地图,但它不符合我的要求。我想要的是解析json,以便可以将其转换为表格格式,例如csv。
  • @Andreas 我肯定会尝试,但我想要的是解析 json 以便将其转换为表格格式,例如 csv。

标签: java json jackson gson jsonobject


【解决方案1】:

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

【讨论】:

  • 我同意结果。但是我需要的是一种通用的方法,即使更改了键也可以使用。如果我们一起使用具有不同键的不同 json 文件,它应该可以工作。在上面的代码中,键是在代码中指定的。我们可以在不指定键的情况下执行 if 吗?
  • 如果它们本质上是完全动态的,那么您打算如何处理并非所有字段都存在于所有对象中的数据?您需要扫描所有内容以构建完整的字段列表,然后才能真正开始编写 CSV。 --- 您将如何处理多个字段为列表的对象?笛卡尔连接?
  • 我必须弄清楚这一点。但首先我必须通过从 json 中提取所有可能的键来构建 csv 模式。
  • 看看这个网站konklone.io/json 并使用我上面提供的json,你会更好地理解我想说的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-14
  • 1970-01-01
  • 1970-01-01
  • 2018-10-21
相关资源
最近更新 更多