我试图在Java上使用gson将大量自定义对象写入json文件,但它在文件完成之前就中断了?

xu3bshqb  于 11个月前  发布在  Java
关注(0)|答案(2)|浏览(179)

我正在编写一个Java程序来读取MTGJson,方法是将每张卡片保存为一个自定义类,然后使用gson将所有这些卡片写入另一个JSON文件。(我发现我不能使用gson从mtgjson中读取,但我可以使用json.simple。)看起来代码以一种奇怪的顺序读取对象,然后不保存每张卡片到列表中,然后在整个列表用json编写之前,json文件的下载在这里。https://www.mtgjson.com/json/AllSets.json

import com.google.gson.Gson;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class DatabaseTrimmer {

    public static void main(String[] args) throws IOException, ParseException {
        JSONObject ALLSETS = (JSONObject) new JSONParser().parse(new FileReader("AllSets.json"));
        List<MTGCard> AllCards = new ArrayList<>();
        for(Object o: ALLSETS.values()) {
            JSONObject set = (JSONObject)o;
            JSONArray cards = (JSONArray) (set.get("cards"));
            for (Object card : cards) {
                MTGCard tempCard = new MTGCard((JSONObject) card);
                if (!tempCard.isReprint) {
                    AllCards.add(tempCard);
                }
            }
        }
        Gson gson = new Gson();
        FileWriter writer = new FileWriter("TrimmedAllSets.json");
        gson.toJson(AllCards, writer);
    }
}

字符串
最后一张要保存到AllCards列表的卡片是“Volrath's Motion Sensor”,这是某个集合的最后一张卡片,但不是最后一张卡片。MTGCard类存储了每张卡片的许多属性,如名称和文本。输出json的结尾看起来像这样:

"isReprint":false,"printings":["UGL"]},{"name":"Sorry","type":"Enchantment","text":"Before any player casts a spell with the same name as a card in any graveyard, that player may say \"sorry.\" If they don\u0027t, any other player may say \"sorry\" as the spell is being cast. When another player says \"sorry\" this way, counter the spell.\nWhenever a player says \"sorry\" at any other time, Sorry deals 2 damage to that player.","rarity":"uncommon","manaCost":"{U}{U}","artist":"Kaja Foglio","convertedManaCost":2.0,"isFoil":false,"isReprint":false,"printings":["UGL"]},{"name":"Spark Fiend","type":"Creature — Beast","text":"When Spark Fiend enters the battlefield, roll two six-sided dice. If you rolled 2, 3, or 12, sacrifice Spark Fiend. If you rolled 7 or 11, don\u0027t roll dice for Spark Fiend during any of your following upkeeps. If you rolled any other total, note that total.\nAt the beginning of your upkeep, roll two six-sided dice. If you rolled 7, sacrifice Spark Fiend. If you roll the noted total, don\u0027t roll dice for Spark Fiend during any of your


正如你所看到的,它只是切断了。我查找了任何关于gson写限制的东西,但找不到任何东西。而且,我试图让代码从另一个点开始,或者读一个更小的部分,但它仍然任意切断。

ckx4rj1h

ckx4rj1h1#

程序在写入器中的缓冲区被刷新到操作系统之前退出。您应该正确地flush写入器或close()。

yvt65v4c

yvt65v4c2#

先转换成字符串,然后保存在文件中是帮助我-

String allCardsString = gson.toJson(AllCards, writer);
File file = new File("TrimmedAllSets.json");
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(allCardsString.getBytes(StandardCharsets.UTF_8));

字符串

相关问题