Java 如何在Spring Boot中解析json并写入存储库

Java 如何在Spring Boot中解析json并写入存储库,java,json,spring,spring-boot,Java,Json,Spring,Spring Boot,我试图解析历史时间序列数据的JSON API响应,可能有数千行代码。答复的格式如下: { "name": "AAPL", "history": { "2019-03-05": { "open": "175.94", "close": "175.53", "high": "176.00", "low": "174.54", "volume": "1

我试图解析历史时间序列数据的JSON API响应,可能有数千行代码。答复的格式如下:

{
    "name": "AAPL",
    "history": {
        "2019-03-05": {
            "open": "175.94",
            "close": "175.53",
            "high": "176.00",
            "low": "174.54",
            "volume": "19163899"
        },
        "2019-03-04": {
            "open": "175.69",
            "close": "175.85",
            "high": "177.75",
            "low": "173.97",
            "volume": "27436203"
        }
    }
} 
我想将响应写入Spring存储库。我有一个简单的代码来实现这一点,下面显示了一个部分:

RestTemplate restTemplate = new RestTemplate();
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = (JsonObject) jsonParser.parse(result);
JsonElement jsonElement = jsonObject.get("history");
Set<Map.Entry<String, JsonElement>> entrySet = jsonElement.getAsJsonObject().entrySet();

for (Map.Entry<String, JsonElement> entry : entrySet) {
    StockHistory stockHistory = new StockHistory();
    stockHistory.setSymbol(stk);
    // .... Other code
}
RestTemplate RestTemplate=new RestTemplate();
JsonParser JsonParser=新的JsonParser();
JsonObject JsonObject=(JsonObject)jsonParser.parse(结果);
JsonElement=jsonObject.get(“历史”);
Set entrySet=jsonElement.getAsJsonObject().entrySet();
for(Map.Entry:entrySet){
StockHistory StockHistory=新的StockHistory();
股票历史。设置符号(stk);
//……其他代码
}

我根据JSON响应设置对象属性,将对象添加到列表中,最后将列表保存到存储库中。这个过程非常慢,可能是因为我正在为JSON返回中的每个元素创建一个新的
StockHistory
对象。我想知道是否有更好的方法。因为您无法修改JSON结构。我想添加以下代码来解析您在一个名为
Repo
的简单类中提供的JSON。为了做到这一点,你需要我用过的

现在需要在代码中添加以下类

public class Repo {
    public String name;
    public ArrayList<History> histories;

    public Repo() {
        histories = new ArrayList<History>();
    }
}

public class History {
    public String date;
    public HistoryElements elements;
}

public class HistoryElements {
    public String volume;
    public String high;
    public String low;
    public String close;
    public String open;
}
只需使用
RepoParser
类,如下所示

try {
    Repo repo = RepoParser.parseRepo(jsonString);
} catch (Exception e) {
    e.printStackTrace();
}
try {
    while(there is no repo left for parsing) {
        Repo repo = RepoParser.parseRepo(jsonString);
        repoList.add(repo)
    }

    yourRepository.save(repoList); // Save all values at once

} catch (Exception e) {
    e.printStackTrace();
}
为了方便,我也有

更新

您可以考虑将所有的<代码> RePO <代码>添加到一个列表中,然后使用存储库的<代码>保存< /代码>方法将它们全部保存到您的数据库中。 因此,代码应该如下所示

try {
    Repo repo = RepoParser.parseRepo(jsonString);
} catch (Exception e) {
    e.printStackTrace();
}
try {
    while(there is no repo left for parsing) {
        Repo repo = RepoParser.parseRepo(jsonString);
        repoList.add(repo)
    }

    yourRepository.save(repoList); // Save all values at once

} catch (Exception e) {
    e.printStackTrace();
}

希望有帮助

因为您无法修改JSON结构。我想添加以下代码来解析您在一个名为
Repo
的简单类中提供的JSON。为了做到这一点,你需要我用过的

现在需要在代码中添加以下类

public class Repo {
    public String name;
    public ArrayList<History> histories;

    public Repo() {
        histories = new ArrayList<History>();
    }
}

public class History {
    public String date;
    public HistoryElements elements;
}

public class HistoryElements {
    public String volume;
    public String high;
    public String low;
    public String close;
    public String open;
}
只需使用
RepoParser
类,如下所示

try {
    Repo repo = RepoParser.parseRepo(jsonString);
} catch (Exception e) {
    e.printStackTrace();
}
try {
    while(there is no repo left for parsing) {
        Repo repo = RepoParser.parseRepo(jsonString);
        repoList.add(repo)
    }

    yourRepository.save(repoList); // Save all values at once

} catch (Exception e) {
    e.printStackTrace();
}
为了方便,我也有

更新

您可以考虑将所有的<代码> RePO <代码>添加到一个列表中,然后使用存储库的<代码>保存< /代码>方法将它们全部保存到您的数据库中。 因此,代码应该如下所示

try {
    Repo repo = RepoParser.parseRepo(jsonString);
} catch (Exception e) {
    e.printStackTrace();
}
try {
    while(there is no repo left for parsing) {
        Repo repo = RepoParser.parseRepo(jsonString);
        repoList.add(repo)
    }

    yourRepository.save(repoList); // Save all values at once

} catch (Exception e) {
    e.printStackTrace();
}

希望有帮助

经过一些研究,我发现问题出在hibernate上。据我所知,hibernate的一个有用特性是它缓存对象,但当创建大量对象以供插入时,这会导致问题。通过使用spring.jpa.properties.hibernate.jdbc.batch_size属性并在实体类中使用序列生成器进行批处理,可以解决此问题。现在保存数千行的列表要快得多。

经过一些研究,我发现问题出在hibernate上。据我所知,hibernate的一个有用特性是它缓存对象,但当创建大量对象以供插入时,这会导致问题。通过使用spring.jpa.properties.hibernate.jdbc.batch_size属性并在实体类中使用序列生成器进行批处理,可以解决此问题。现在保存数千行的列表要快得多。

我不确定我是否理解建议的类结构。json响应中的日期每天可以追溯到20年前。请解释。我已经更新了我的答案。你能调查一下吗?我无法控制Json文件。我正在使用来自internet数据提供商的REST API。我不想挑剔,但这
公共类2019-03-04
在Java中不是有效的类命名。@makasu,我已经更新了答案,可以解析您的JSON字符串。你能尝试在你的代码中添加我建议并提供链接的库吗?然后,在添加我上面编写的类之后,您需要尝试我的解析器。如果有问题,请告诉我。我已经在我这边测试了代码,它运行得非常好。谢谢:)我不确定我是否理解建议的班级结构。json响应中的日期每天可以追溯到20年前。请解释。我已经更新了我的答案。你能调查一下吗?我无法控制Json文件。我正在使用来自internet数据提供商的REST API。我不想挑剔,但这
公共类2019-03-04
在Java中不是有效的类命名。@makasu,我已经更新了答案,可以解析您的JSON字符串。你能尝试在你的代码中添加我建议并提供链接的库吗?然后,在添加我上面编写的类之后,您需要尝试我的解析器。如果有问题,请告诉我。我已经在我这边测试了代码,它运行得非常好。谢谢:)你能检查一下最新的答案并告诉我这是否有帮助吗?谢谢。:)你能检查一下更新后的答案并告诉我是否有用吗?谢谢。:)