简体   繁体   English

如何修改大型json字符串?

[英]How do I modify a large json string?

Dead silence! 死一般的寂静! Not often you experience that on Stackoverflow... I've added a small bounty to get things going! 你经常在Stackoverflow上体验到这一点......我已经添加了一笔不小的奖金来推动事情发展!

I've built a json document containing information about the location of various countries. 我已经建立了一个json文档,其中包含有关各个国家/地区的信息。 I have added some custom keys. 我添加了一些自定义键。 This is the beginning of the json-file: 这是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,...

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. 实际值保存在从Excel文档中提取的.csv文件中。

I eg 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. 现在我想将这些值与json文件中的正确键匹配。 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. 另一种选择是java的json库,它以某种方式支持我想要的东西。 I havent been able to find an easy solution for it yet. 我还没有找到一个简单的解决方案。 The document is pretty large ~ 10MB, if it makes any difference! 该文件相当大~10MB,如果它有任何区别!

EDIT: I've used QGIS to manipulate the .shp file, so some kind of extension could be of use too. 编辑:我已经使用QGIS来操作.shp文件,所以某种扩展也可以使用。

Just convert both the JSON and the CSV to a fullworthy Java object. 只需将JSON和CSV转换为完全可用的Java对象即可。 This way you can write any Java logic to your taste to alter the Java objects depending on the one or other. 通过这种方式,您可以根据自己的喜好编写任何Java逻辑,以根据一个或另一个更改Java对象。 Finally convert the modified Java object representing the JSON data back to a JSON string. 最后,将表示JSON数据的已修改Java对象转换回JSON字符串。

There is however one problem in your JSON. 但是你的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. / in banned/censored不是JSON字段名称的有效字符,因此许多现有的JSON反序列化器可能会阻塞它。 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. 我建议使用Google Gson进行JSON和Java之间的转换。 Here's a kickoff example based on your JSON structure (with banned/censored renamed to bannedOrCensored ): 这是基于您的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;
}

You only need to add/generate getters and setters yourself. 您只需自己添加/生成getter和setter。 Then, you'll be able to convert between JSON and Java like follows: 然后,您将能够在JSON和Java之间进行转换,如下所示:

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 . 要在CSV和Java对象之间进行转换,只需选择许多CSV解析器中的一个,如OpenCSV You can even homegrow your own with help of BufferedReader . 您甚至可以在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: 最后,在更改表示JSON数据的Java对象之后,您可以在Gson的帮助下将其转换回JSON字符串,如下所示:

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

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. 虽然BalusC的回答告诉你如何在当前的设置中做到这一点,但我有一个更激进的建议:摆脱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". 根据想法,JSON并不意味着存储数据 - 它旨在用作“基于文本的轻量级开放标准,专为人类可读的数据交换而设计”。 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. 如果您不想要功能齐全的数据库,请使用HSQLDB或JavaDB之类的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM