相关文章推荐
干练的硬币  ·  readline()— ...·  2 年前    · 
成熟的脸盆  ·  vue-cli4 引入 ...·  3 年前    · 
干练的红薯  ·  KonvaJS 原理解析 - 掘金·  3 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I tried to save data to json string in a txt file using Gson and then restore it using Gson either. Things go well if I do it in eclipse. But when packaged to jar, Gson throws Exceptions.

Here is the code for saving the file.

String gsonStr = gson.toJson(masterShips);  // masterShips is ArrayList<Ship>
BufferedWriter writer = null;
try {
    writer = new BufferedWriter(new FileWriter("D:\\master_ship.txt"));
    writer.write(gsonStr);
} catch (IOException e) {
    System.err.println(e);
} finally {
    if (writer != null) {
        try {
            writer.close();
        } catch (IOException e) {
            System.err.println(e);

Then I read the file in eclipse using this code (and it works):

Scanner in = new Scanner(new FileReader("D:\\master_ship.txt"));
String str = in.nextLine();
Log.toDebug(str);
in.close();
JsonParser parser = new JsonParser();
JsonElement je = parser.parse(str);
JsonArray ja = je.getAsJsonArray();
for (int i=0; i<ja.size(); ++i) { 

But after packaged into jar and run in cmd, Exception occurs:

Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.
stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malform
ed JSON at line 1 column 4
        at com.google.gson.JsonParser.parse(JsonParser.java:65)
        at com.google.gson.JsonParser.parse(JsonParser.java:45)
        at kan.util.Master.loadMasterShip(Master.java:44)
        at kan.util.Master.load(Master.java:27)
        at kan.Main.main(Main.java:22)
Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLeni
ent(true) to accept malformed JSON at line 1 column 4
        at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1505)
        at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1386)
        at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:531)
        at com.google.gson.stream.JsonReader.peek(JsonReader.java:414)
        at com.google.gson.JsonParser.parse(JsonParser.java:60)
        ... 4 more

According to the hint of the Exception, I changed my code and it still works in eclipse:

Scanner in = new Scanner(new FileReader("D:\\master_ship.txt"));
String str = in.nextLine();
in.close();
Reader reader = new StringReader(str);
JsonReader jr = new JsonReader(reader);
jr.setLenient(true);
JsonParser parser = new JsonParser();
JsonElement je = parser.parse(jr);
JsonArray ja = je.getAsJsonArray();
for (int i=0; i<ja.size(); ++i) { 

But jar failed and throws

Exception in thread "main" java.lang.IllegalStateException: This is not a JSON A
rray.
    at com.google.gson.JsonElement.getAsJsonArray(JsonElement.java:106)
    at kan.util.Master.loadMasterShip(Master.java:58)
    at kan.util.Master.load(Master.java:30)
    at kan.Main.main(Main.java:22)

As suggested by Sotirios I cut the length of the arraylist down, and when I increase the number of ships to 4, things go wrong. Here is the json:

[{"id":1,"name":"睦月","type":2,"rank":2,"fuelMax":15,"bulletMax":15,"slotNum":2,"speed":10,"afterLv":20,"afterId":254,"range":1,"powerups":[1,1,0,0]},{"id":2,"name":"如月","type":2,"rank":1,"fuelMax":15,"bulletMax":15,"slotNum":2,"speed":10,"afterLv":20,"afterId":255,"range":1,"powerups":[0,1,0,0]},{"id":6,"name":"長月","type":2,"rank":1,"fuelMax":15,"bulletMax":15,"slotNum":2,"speed":10,"afterLv":20,"afterId":258,"range":1,"powerups":[0,1,0,0]},{"id":7,"name":"三日月","type":2,"rank":1,"fuelMax":15,"bulletMax":15,"slotNum":2,"speed":10,"afterLv":20,"afterId":260,"range":1,"powerups":[0,1,0,0]}]
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ↑ colunm 473
Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.
stream.MalformedJsonException: Unterminated object at line 1 column 473
        at com.google.gson.internal.Streams.parse(Streams.java:56)
        at com.google.gson.JsonParser.parse(JsonParser.java:84)
        at kan.util.Master.loadMasterShip(Master.java:55)
        at kan.util.Master.load(Master.java:30)
        at kan.Main.main(Main.java:22)
Caused by: com.google.gson.stream.MalformedJsonException: Unterminated object at
 line 1 column 473
        at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1505)
        at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:480)
        at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:403)
        at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:
        at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:
        at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:
        at com.google.gson.internal.Streams.parse(Streams.java:44)
        ... 4 more

Can anyone help me with this, you will be reaally preciated!

@SotiriosDelimanolis It is really a long json, I don't know how to show you. It is a ArrayList of several hundred ships with more than 10 params. – bijiDango Dec 26, 2013 at 2:39 @SotiriosDelimanolis You used the json I just added? With which parser code? I can't find what's wrong with the json at the column the Exception mentioned – bijiDango Dec 26, 2013 at 3:10 I used the json you posted with the code right before you state But jar failed and throws. – Sotirios Delimanolis Dec 26, 2013 at 3:15

just add below code where u parse

String strJson = "[{\"id\":1,\"name\":\"睦月\",\"type\":2,\"rank\":2,\"fuelMax\":15,\"bulletMax\":15,\"slotNum\":2,\"speed\":10,\"afterLv\":20,\"afterId\":254,\"range\":1,\"powerups\":[1,1,0,0]},{\"id\":2,\"name\":\"如月\",\"type\":2,\"rank\":1,\"fuelMax\":15,\"bulletMax\":15,\"slotNum\":2,\"speed\":10,\"afterLv\":20,\"afterId\":255,\"range\":1,\"powerups\":[0,1,0,0]},{\"id\":6,\"name\":\"長月\",\"type\":2,\"rank\":1,\"fuelMax\":15,\"bulletMax\":15,\"slotNum\":2,\"speed\":10,\"afterLv\":20,\"afterId\":258,\"range\":1,\"powerups\":[0,1,0,0]},{\"id\":7,\"name\":\"三日月\",\"type\":2,\"rank\":1,\"fuelMax\":15,\"bulletMax\":15,\"slotNum\":2,\"speed\":10,\"afterLv\":20,\"afterId\":260,\"range\":1,\"powerups\":[0,1,0,0]}]";
        GsonResponse gsonResponse = null;
        try {
            gsonResponse = new Gson().fromJson(strJson,
                    GsonResponse.class);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
                Thank you, but yours is really not a good solution considering the effort you put in adding quotation marks. And also I solved this problem. It seems that the Scanner my system use is doing some strange  decoding. When I changed the loading scheme to InputStream it works correctly.
– bijiDango
                Mar 11, 2014 at 9:50
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.