如何修改一个大的json字符串? [英] How do I modify a large json string?

查看:184
本文介绍了如何修改一个大的json字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

死沉默!不太经常在Stackoverflow上体验...我已经添加了一个小钱来让事情发生。

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

我已经创建了一个包含信息的json文档关于各国的位置。我添加了一些自定义键。这是json文件的开头:

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,...

所有的自定义键(如bombed,审查等)有价值,但他们只是老(假的,如果你想要的)价值观。实际值保存在从excel文档中提取的.csv文件中。

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.

具有:

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



现在我想将这些值与json文件中的正确键相匹配。有没有任何程序,我可以使用?另一个选项是java的json库,它以某种方式支持我想要的。我还没有找到一个容易的解决方案。文档很大〜10MB,如果它有什么区别!

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!

编辑:我使用了

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

推荐答案

只需将JSON和CSV都转换为完整的Java对象即可。这样,你可以编写任何Java逻辑到你的口味来改变Java对象,取决于一个或其他。最后将表示JSON数据的修改后的Java对象转换回JSON字符串。

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.

然而,JSON中有一个问题。 / 在禁止/ censored 中不是JSON字段名称的有效字符,因此许多现有的JSON解串器可能会窒息这个。如果你解决这个问题,你就可以使用其中的一个。

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.

我可以推荐使用 Google Gson 用于JSON和Java之间的转换。这里有一个基于你的JSON结构的启动示例( banned / censored 重命名为 bannedOrCensored ):

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;
}

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

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);

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

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.

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

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);

这篇关于如何修改一个大的json字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆