【问题标题】:JsonObject gson not processing properly convert to string and intJsonObject gson 未正确处理转换为字符串和 int
【发布时间】:2020-11-12 23:49:06
【问题描述】:

所以我现在正在编写一个REST 应用程序,它从邮递员那里接收一些JSON 并对其进行解析,调用我的数据库,然后将对象吐回给邮递员(我知道这基本上只是一个GET 现在。我一直在这样做以查明问题的根源)。

我已经对此进行了几次不同的测试。如果我将res += company;res += dept_id 返回给邮递员,它们都会返回预期值。

但是,如果我执行 getDepartment(company, dept_id),则 func 将失败并且 oldDept 未初始化。如果我对getDepartment("CoolCarsInc",16) 进行硬编码,那么oldDept 将被初始化并按预期显示在邮递员的输出中。

因此,我将 processedJson.get("company")processedJson.get("dept_id") 转换为字符串和 int 的方式似乎极有可能出现问题。这是怎么回事???

JSON:

{
"company":"CoolCarsInc",
"dept_id":16
}

代码:

// ITEM 7.2 - /department PUT
@Path("putdepartment")
@PUT
@Consumes("application/json")
@Produces("application/json")
public Response putDepartment(
  String incomingJson
  ){
  
  //body comes in as a String, Circle is parsing it
  //or you would use String circleIn and parse it yourself
  //do some validation and update in db in either case
  
  DataLayer dl = null;
  Gson gson = new GsonBuilder().setPrettyPrinting().create();
  String res = "";
  JsonElement receivedJson = new JsonParser().parse(incomingJson);
  JsonObject processedJson = receivedJson.getAsJsonObject();

  String company = processedJson.get("company").toString();
  String dept_name = processedJson.get("dept_name").toString();
  String dept_no = processedJson.get("dept_no").toString();
  String location = processedJson.("location").toString();
  int dept_id = processedJson.get("dept_id").getAsInt;
  
  try {
     dl = new DataLayer(processedJson.get("company"));
     
     Department oldDept = dl.getDepartment(company, dept_id);
     oldDept.setCompany(company);
     oldDept.setDeptName(dept_name);
     oldDept.setDeptNo(dept_no);
     oldDept.setLocation(location);
     oldDept.setId(dept_id);
     dl.updateDepartment(oldDept);

     Department updatedDep = dl.getDepartment(company, dept_id);
     res += gson.toJson(updatedDep);
     
   } catch (Exception e) {
     return Response.status(Response.Status.NOT_FOUND).build();
  } finally {
     dl.close();
  }
  
  return Response.ok("updated department:\n" + res).build();

} 

【问题讨论】:

  • 你能澄清一下你的问题吗?如果你这样做 dl.getDepartment(1, 1); 它可以工作。但是,如果你在company=1dept_id=1 的位置执行dl.getDepartment(company, dept_id);,它会失败吗?这与 JSON 或 Gson 无关,不是吗?请给出确切的代码哪些有效,哪些无效。
  • @pirho 对此感到抱歉,谢谢!我修改了问题以显示确切的代码并更好地说明问题。你是正确的,除了 company 是一个字符串而不是一个 int。除此之外,我希望问题更清楚。感谢您的帮助!

标签: java json parsing gson tostring


【解决方案1】:

我不确定 gson。但是你也可以使用java的org.json库。

import org.json.JSONArray;
import org.json.JSONObject;

然后将字符串包装成 JSONObject。

JSONObject json = new JSONObject(incomingJson);

然后您可以通过以下方式访问密钥:

json.getString("company");
json.getInt("dept_id");

为了使用这个库,在 pom.xml 中包含依赖:

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20190722</version>
        </dependency>

【讨论】:

    猜你喜欢
    • 2018-05-07
    • 1970-01-01
    • 2019-11-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    相关资源
    最近更新 更多