如何修改一个大的 json 字符串?

发布于 2024-09-13 04:14:12 字数 1017 浏览 3 评论 0原文

死一般的寂静!您在 Stackoverflow 上不会经常遇到这种情况...我添加了一个小赏金来让事情顺利进行!

我已经构建了一个 json 文档,其中包含有关各个国家/地区位置的信息。我添加了一些自定义键。这是 json 文件的开头:

{
    "type": "FeatureCollection",
    "features": [
        { "type": "Feature", "properties": {
            "NAME": "Antigua and Barbuda", 
            "banned/censored": "AG",   
            "Bombed": 29, 
            "LON": -61.783000, "LAT": 17.078000 },
    "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -61.686668,...

所有自定义键(如轰炸、禁止/审查等)都有值,但它们只是旧值(如果需要,可以是假值)。实际值保存在从 Excel 文档中提取的 .csv 文件中。

例如,我有这样的:

                            banned/censored     bombed   
Antigua and Barbuda              2                 120
...

现在我想将这些值与 json 文件中的正确键进行匹配。有没有我可以使用的程序?另一个选择是 java 的 json 库,它以某种方式支持我想要的。我还没有找到一个简单的解决方案。该文档相当大~ 10MB,如果有什么区别的话!

编辑:我使用 QGIS 来操作 .shp 文件,因此可以使用某种扩展名也。

Dead silence! Not often you experience that on Stackoverflow... I've added a small bounty to get things going!

I've built a json document containing information about the location of various countries. I have added some custom keys. This is the beginning of the json-file:

{
    "type": "FeatureCollection",
    "features": [
        { "type": "Feature", "properties": {
            "NAME": "Antigua and Barbuda", 
            "banned/censored": "AG",   
            "Bombed": 29, 
            "LON": -61.783000, "LAT": 17.078000 },
    "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -61.686668,...

All the custom keys (like bombed, banned/censored etc.) have values, but they are just old (bogus if you want) values. The real values are kept in a .csv file extracted from a excel document.

I e.g. have this:

                            banned/censored     bombed   
Antigua and Barbuda              2                 120
...

Now I want to match these values with the proper key in the json-file. Is there any programs out there that I can use? Another option would be a json library for java, which somehow supports what I want. I havent been able to find an easy solution for it yet. The document is pretty large ~ 10MB, if it makes any difference!

EDIT: I've used QGIS to manipulate the .shp file, so some kind of extension could be of use too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

z祗昰~ 2024-09-20 04:14:12

只需将 JSON 和 CSV 转换为完整的 Java 对象即可。通过这种方式,您可以根据自己的喜好编写任何 Java 逻辑,以根据其中之一更改 Java 对象。最后将修改后的表示 JSON 数据的 Java 对象转换回 JSON 字符串。

然而,您的 JSON 中有一个问题。 banned/censored 中的 / 不是 JSON 字段名称的有效字符,因此许多现有的 JSON 反序列化器可能会对此感到困惑。如果你解决了这个问题,那么你就可以使用其中之一了。

我建议使用 Google Gson 在 JSON 和 Java 之间进行转换。下面是一个基于 JSON 结构的启动示例(将 banned/censored 重命名为 bannedOrCensored):

class Data {
    private String type;
    private List<Feature> features;
}

class Feature {
    private String type;
    private Properties properties;
    private Geometry geometry;
}

class Properties {
    private String NAME;
    private String bannedOrCensored;
    private Integer Bombed;
    private Double LON;
    private Double LAT;
}

class Geometry {
    private String type;
    private Double[][][][] coordinates;
}

您只需自己添加/生成 getter 和 setter。然后,您将能够在 JSON 和 Java 之间进行转换,如下所示:

Data data = new Gson().fromJson(jsonString, Data.class);

要在 CSV 和 Java 对象之间进行转换,只需选择众多 CSV 解析器之一,例如 OpenCSV。您甚至可以在 BufferedReader 的帮助下自行开发。

最后,在更改表示 JSON 数据的 Java 对象后,您可以借助 Gson 将其转换回 JSON 字符串,如下所示:

String json = new Gson().toJson(data);

Just convert both the JSON and the CSV to a fullworthy Java object. This way you can write any Java logic to your taste to alter the Java objects depending on the one or other. Finally convert the modified Java object representing the JSON data back to a JSON string.

There is however one problem in your JSON. The / in banned/censored is not a valid character for a JSON field name, so many of the existing JSON deserializers may choke on this. If you fix this, then you'll be able to use one of them.

I can recommend using Google Gson for the converting between JSON and Java. Here's a kickoff example based on your JSON structure (with banned/censored renamed to bannedOrCensored):

class Data {
    private String type;
    private List<Feature> features;
}

class Feature {
    private String type;
    private Properties properties;
    private Geometry geometry;
}

class Properties {
    private String NAME;
    private String bannedOrCensored;
    private Integer Bombed;
    private Double LON;
    private Double LAT;
}

class Geometry {
    private String type;
    private Double[][][][] coordinates;
}

You only need to add/generate getters and setters yourself. Then, you'll be able to convert between JSON and Java like follows:

Data data = new Gson().fromJson(jsonString, Data.class);

To convert between CSV and a Java object, just pick one of the many CSV parsers, like OpenCSV. You can even homegrow your own with help of BufferedReader.

Finally, after altering the Java object representing the JSON data, you can convert it back to JSON string with help of Gson as follows:

String json = new Gson().toJson(data);
春夜浅 2024-09-20 04:14:12

虽然 BalusC 的答案告诉您如何在当前设置中执行此操作,但我有一个更激进的建议:摆脱 JSON。

根据想法,JSON 并不意味着存储数据 - 它旨在用作“为人类可读的数据交换而设计的基于轻量级文本的开放标准”。也就是说:

  • 低流量(尽可能少的无意义数据)
  • 人类可读
  • 易于使用动态语言处理

另一方面,数据存储的要求比这多得多。这就是数据库存在的原因。因此,将您的存储移动到数据库。如果您不需要功能齐全的数据库,请使用 HSQLDB 或 JavaDB 之类的数据库。

While BalusC's answer tells you how to do it in your current setup, I have a more radical suggestion: get rid of the JSON.

By idea JSON is not meant to store data - it is meant to be used as a "lightweight text-based open standard designed for human-readable data interchange". That is:

  • low-traffic (as little non-meaningful data as possible)
  • human-readable
  • easy to handle with dynamic languages

Data storages on the other hand have much more requirements than this. That's why databases exist. So move your storage to a database. If you don't want a full-featured database, use something like HSQLDB or JavaDB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文