简体   繁体   English

JsonObject gson 未正确处理转换为字符串和整数

[英]JsonObject gson not processing properly convert to string and int

So I'm writing a REST app right now that receives some JSON from postman and parses it, calls my database, and then spits the object back up to postman (I'm aware this is basically just a GET right now. I've been doing this to get to the bottom of the issue). So I'm writing a REST app right now that receives some JSON from postman and parses it, calls my database, and then spits the object back up to postman (I'm aware this is basically just a GET right now. I've一直这样做是为了解决问题)。

I've tested this a few different times.我已经对此进行了几次不同的测试。 If I return res += company;如果我返回res += company; or res += dept_id back to postman, they both come back with their expected values.res += dept_id返回 postman,它们都返回预期值。

However, if I do my getDepartment(company, dept_id) , the func fails and oldDept is not initialized.但是,如果我执行getDepartment(company, dept_id) ,则 func 失败并且oldDept未初始化。 If I hardcode getDepartment("CoolCarsInc",16) then oldDept is initialized and shows up as expected in the output from postman.如果我getDepartment("CoolCarsInc",16)进行硬编码,则oldDept将被初始化并按预期显示在 postman 的 output 中。

So it seems extremely likely that there's something wrong with how I am converting processedJson.get("company") and processedJson.get("dept_id") to a string and int.因此,我将processedJson.get("company")processedJson.get("dept_id")转换为字符串和int的方式似乎极有可能出现问题。 What is going on here???这里发生了什么???

JSON: JSON:

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

Code:代码:

// 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();

} 

I am not sure about gson.我不确定 gson。 But you can also use org.json library of java.但您也可以使用 java 的 org.json 库。

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

Then wrap string into JSONObject.然后将字符串包装到 JSONObject 中。

JSONObject json = new JSONObject(incomingJson);

Then you can access the keys as:然后您可以按以下方式访问密钥:

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

For using this library include dependency in pom.xml as:为了使用这个库,在 pom.xml 中包含依赖项:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM