【问题标题】:org.json JSONObject adding extra object to JSONObject while returning it from spring boot controllerorg.json JSONObject 在从 Spring Boot 控制器返回时向 JSONObject 添加额外的对象
【发布时间】:2018-10-12 07:47:49
【问题描述】:

我正在从 Spring Boot 控制器 (1.5.16.RELEASE) 返回 org.json JSONObject。我得到了一个额外的地图对象。

我的代码-

@GetMapping(value = Constants.API_LOGIN)
    public Object login(@RequestParam String userName, @RequestParam String password) throws JSONException  {

        UserAuth userAuth = new UserAuth();
        UserAuth user = null;

        try {
            Preconditions.checkArgument(!Strings.isNullOrEmpty(userName), "empty UserName");
            Preconditions.checkArgument(!Strings.isNullOrEmpty(password), "empty password");

            userAuth.setUserName(userName);
            userAuth.setPassword(password);
            user = authService.checkAuth(userAuth);

        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }

        JSONObject json = new JSONObject();

        if (user != null) {
            json.put("status", true);
            json.put("message", "login success");
            return ResponseEntity.status(HttpStatus.OK).body(json);
        } else {
            json.put("status", false);
            json.put("message", "username or password doesnt match");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(json);
        }
    }

pom.xml

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

预期的 Json

 {
        "message": "login success",
        "status": true
 }

我正在获取 JSON

{
    "map": {
        "message": "login success",
        "status": true
    }
}

我不知道为什么我的 JSONObject 中有额外的地图对象。

【问题讨论】:

  • 你能否展示你完整的控制器方法..你在哪里返回 Json 对象
  • 添加了完整的控制器方法@kj007

标签: java spring-boot org.json


【解决方案1】:

我认为问题在于默认的 Springboot JSON 序列化程序是 Jackson,它不知道如何处理 JSONObject,所以它将它包装成一个 Map&lt;String, Object&gt;,其中键是 map,值是你的JSONObject。 在您的情况下,仅使用地图会简单得多:

@GetMapping(value = Constants.API_LOGIN)
public ResponseEntity<Object> login(@RequestParam String userName, @RequestParam String password) throws JSONException  {
    .....
    Map<String, Object> json = new HashMap<>();
    json.put("status", true);
    json.put("message", "login success");
    return ResponseEntity.status(HttpStatus.OK).body(json);
}

或者我想另一种解决方案是在正文中简单地返回一个字符串棒json.toString

@GetMapping(value = Constants.API_LOGIN)
public String login(@RequestParam String userName, @RequestParam String password) throws JSONException  {
    ....
    JSONObject json = new JSONObject();
    json.put("status", true);
    json.put("message", "login success");
    return ResponseEntity.status(HttpStatus.OK).body(json.toString());
}

【讨论】:

  • @gowthamg 酷!请投票并接受答案!
猜你喜欢
  • 1970-01-01
  • 2019-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-24
  • 1970-01-01
  • 2015-05-16
相关资源
最近更新 更多