简体   繁体   English

为什么我的JSONObject(String)返回空?

[英]Why is my JSONObject(String) returning empty?

So, I'm receiving from a post method a JSON, like that shown below (a JSON filled with arrays of strings). 所以,我从post方法接收一个JSON,如下所示(一个充满字符串数组的JSON)。 I receive it in my Java method as a string. 我在Java方法中以字符串形式接收它。 My goal is to convert it to a JSONObject and to iterate about it, for example: x.getString("0") 我的目标是将其转换为JSONObject并对其进行迭代,例如: x.getString("0")

But I have a problem, when I try to convert to a JSONObject, I am using import org.json.JSONObject , it returns an empty JSONObject like this : {} Why is it happening ? 但我有一个问题,当我尝试转换为JSONObject时,我使用import org.json.JSONObject ,它返回一个空的JSONObject,如下所示:{}为什么会发生? Thanks 谢谢

EDIT: And the string is received with success because if I do return, it returns the JSON I've sent. 编辑:字符串收到成功,因为如果我返回,它返回我发送的JSON。

{ '0': [ 'Mon Apr 08 2019 19:26:37 GMT+0000 (UTC)' ],
  '1': [ '1234', '456', '1234', '456', '1234', '456', '545' ],
  '2': [ '1234', '456', '1234', '456', '1234', '456', '545' ],
  '3': [ '1234', '456', '1234', '456', '1234', '456', '545' ],
  '4': [ '1234', '456', '1234', '456', '1234', '456', '545' ],
  '5': [ 'Mon Apr 08 2019 19:30:00 GMT+0000 (UTC)' ] }
 // My Java method that converts JSON received to JSONObject:

@POST
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/{nifCliente}") // irrelevant this part, ignore it, important is the String
    public JSONObject inserir (@PathParam("nifCliente")int c, String d) {
           JSONObject f = new JSONObject(d)
       return f;

    }

In your current implementation and JSON structure, in order to get the value, you should use f.get("0") and not f.getString("0"). 在当前的实现和JSON结构中,为了获取值,您应该使用f.get(“0”)而不是f.getString(“0”)。 Like this: 像这样:

        String json = "{ '0': [ 'Mon Apr 08 2019 19:26:37 GMT+0000 (UTC)' ],\n" +
            "  '1': [ '1234', '456', '1234', '456', '1234', '456', '545' ],\n" +
            "  '2': [ '1234', '456', '1234', '456', '1234', '456', '545' ],\n" +
            "  '3': [ '1234', '456', '1234', '456', '1234', '456', '545' ],\n" +
            "  '4': [ '1234', '456', '1234', '456', '1234', '456', '545' ],\n" +
            "  '5': [ 'Mon Apr 08 2019 19:30:00 GMT+0000 (UTC)' ] }";
    JSONObject f = new JSONObject(json);
    System.out.println(f.get("0"));

The above code should work and the output will be: 上面的代码应该工作,输出将是:

["Mon Apr 08 2019 19:26:37 GMT+0000 (UTC)"] [“Mon Apr 08 2019 19:26:37 GMT + 0000(UTC)”]

Your JSOn is not valid. 您的JSOn无效。 and you don't have the correct header of application/json in your postman collection. 并且您的邮递员集合中没有正确的application / json标头。 Try using this JSON: 尝试使用此JSON:

{
"0": [
    "Mon Apr 08 2019 19:26:37 GMT+0000 (UTC)"
],
"1": [
    "1234",
    "456",
    "1234",
    "456",
    "1234",
    "456",
    "545"
],
"2": [
    "1234",
    "456",
    "1234",
    "456",
    "1234",
    "456",
    "545"
],
"3": [
    "1234",
    "456",
    "1234",
    "456",
    "1234",
    "456",
    "545"
],
"4": [
    "1234",
    "456",
    "1234",
    "456",
    "1234",
    "456",
    "545"
],
"5": [
    "Mon Apr 08 2019 19:30:00 GMT+0000 (UTC)"
]

} }

EDIT 1: 编辑1:

And add the header to POSTMAN 并将标题添加到POSTMAN

Your problem is not in the construction of JSONObject, that works. 你的问题不在于JSONObject的构造,这有效。

Your problem is that when you try to return that JSONObject as @Produces(MediaType.APPLICATION_JSON) the system doesn't know how to parse it. 您的问题是,当您尝试将该JSONObject作为@Produces(MediaType.APPLICATION_JSON)返回时,系统不知道如何解析它。

You try to return the same object you receive and you will see how it works, because @Produces(MediaType.APPLICATION_JSON) knows parse String to Json. 您尝试返回您收到的同一个对象,您将看到它是如何工作的,因为@Produces(MediaType.APPLICATION_JSON)知道解析String到Json。

Then try return f as string: 然后尝试将f作为字符串返回:

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/{nifCliente}") // irrelevant this part, ingore it, important is the String
    public String inserir (@PathParam("nifCliente")int c, String d) {
           JSONObject f = new JSONObject(d);

           return f.toString();

    }

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

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