【问题标题】:Gson.fromJson stuck on lineGson.fromJson 卡在了线上
【发布时间】:2020-06-06 20:52:29
【问题描述】:

我正在使用 gson 解析来自 HttpExchange 对象的数据。但是,在我的 IDE 中,我在使用 .fromjson 的行设置了一个断点,并且它永远不会超过该行。它只是一次又一次地回到它。

public void handle(HttpExchange exchange) throws IOException {
        System.out.println("/createTest");
        if (exchange.getRequestMethod().toLowerCase().equals("post")){
            System.out.println("get request");

            InputStream reqBody = exchange.getRequestBody();
            String reqData = readString(reqBody);

            Gson gson = new Gson();
            TestRequest testRequest = new TestRequest();
            System.out.println(reqData);
            testRequest = gson.fromJson(reqData, TestRequest.class);
            System.out.println("gson done");

            //Get the test
            TestService testService = new TestService();
            TestResponse result = testService.execute(testRequest);}

这是 TestRequest.java 的更新 java

import Model.TestParameter;

import java.util.ArrayList;
import java.util.HashMap;

public class TestRequest {
private HashMap<ArrayList<Integer>, book> sections;
private int difficulty;
private boolean closedBook;
private int length;
private int assortment;

我使用 JSONlint 来检查我输入的 JSON 数据是否正确,确实如此。在这里,对于好奇的人:

{
  "sections": [
    {
      "chapters": [
        11,
        12,
        20,
        21,
        22
      ],
      "book": "NEPHI1"
    },
    {
      "chapters": [
        20,
        29
      ],
      "book": "NEPHI2"
    },
    {
      "chapters": [
        1
      ],
      "book": "OMNI"
    }
  ],
  "difficulty": 3,
  "closedBook": true,
  "length": 21,
  "assortment": 0
}

【问题讨论】:

  • 在非调试模式下运行时表现如何?
  • 它抛出一个异常(我应该一直在做 try/catch)。这是一个例外:com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 15 path $.sections[0]
  • 你能用TestRequest的源代码更新问题吗?
  • 完成!你可以在原帖中看到它
  • TestParameter.book的类型是什么?根据java模型看起来sections应该是TestParameter.bookList的映射,但是你的json有字符串"book": "NEPHI1",。尝试将此 json sn-p 更改为 "book": ["NEPHI1"],

标签: javascript java json server gson


【解决方案1】:
private HashMap<ArrayList<Integer>, book> sections;

Gson 认为您的 sections 字段应该是地图...

"sections": [

...但是您有一个项目数组,每个项目都是一个带有chapters(整数数组)和book(字符串)的对象。 MapHashMap 采用两个通用参数,其中第一个是键,第二个是值。在 JSON 中,键几乎总是String,就像在这个 JSON 中表示 HashMap&lt;String, Integer&gt;

{
  "abc": 123,
  "def": 456
}

如果你想匹配你提供的 JSON,你需要sections 看起来像这样:

public class Section {
  private ArrayList<Integer> chapters;
  private String book;
}
private ArrayList<Section> sections;

但是,在我的 IDE 中,我在使用 .fromjson 的行处设置了一个断点,并且它永远不会越过该行。

听起来您在 cmets 中理解try/catch,但是对于您的问题标题“卡在在线”和您的问题中的这句话,我将在这里总结一下。

当 Java 应用程序中发生意外或错误时(如您的 com.google.gson.JsonSyntaxException 中的“异常”),它将查找最近的封闭 try/catch 块。如果当前方法中没有一个,它将检查调用当前方法的方法,然后检查调用 that 方法的方法,以此类推。 Web 服务器应用程序通常有一个封闭的try/catch,这将导致像 500 这样的错误代码,如果根本没有捕获到错误,它可能会终止程序。您可以在Java tutorial "What is an Exception?" 中了解有关异常的更多信息,但它“卡在”该行的外观很可能是由于它进入默认异常处理程序并且从未返回到“抛出”异常的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    相关资源
    最近更新 更多