0

So, I'm receiving from a post method a JSON, like that shown below (a JSON filled with arrays of strings). I receive it in my Java method as a string. My goal is to convert it to a JSONObject and to iterate about it, for example: 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 ? Thanks

EDIT: And the string is received with success because if I do return, it returns the JSON I've sent.

{ '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;

    }

d219
  • 2,707
  • 5
  • 31
  • 36

2 Answers2

1

Your problem is not in the construction of JSONObject, that works.

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.

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.

Then try return f as string:

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

    }
Francesc Recio
  • 2,187
  • 2
  • 13
  • 26
  • Isn't it the same that returning d ? My goal with parsing the json recieved in a and object is to iterate the object and the arrays inside of it to get the strings inside of the arrays in json, and if i put it as a String i can´t iterate it. And i've tried to do it and it returns and empty json like this "{}" . But if I return d it returns what i've sent – Afonso Lobo Apr 21 '19 at 14:38
  • I'm telling you how to return the object so that it returns well. Processing from input to output is an issue that has nothing to do with the question of why JSONObject does not return well. – Francesc Recio Apr 21 '19 at 14:42
  • Yes, you are rigtht, my goal is really to return the JSONObject, not as a String, but it's always returning empty even with all that help :/. I will just try another way to do it instead of parsing the String to JSONObject. Thanks for all the atention. – Afonso Lobo Apr 21 '19 at 14:50
0

In your current implementation and JSON structure, in order to get the value, you should use f.get("0") and not 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)"]

Your JSOn is not valid. and you don't have the correct header of application/json in your postman collection. Try using this 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:

And add the header to POSTMAN

Adi Ohana
  • 927
  • 2
  • 13
  • 18
  • I tried what you´ve said but when i do that the server throws me de 500 error. I think it can´t do the f.get("0") because de JSONObject created it returns empty and i don´t know why. – Afonso Lobo Apr 21 '19 at 11:45
  • can you please share your client code? And the stack trace for the 500 error? – Adi Ohana Apr 21 '19 at 12:00
  • At This moment i'm trying at postman, i'm sending the JSON via body raw, and the header content type os application json. The only Code i have is the one above. – Afonso Lobo Apr 21 '19 at 12:37
  • ok, can you please share the postman request? You can use postman "Code" feature to export your request. I suspect that the issue is with the String escaping – Adi Ohana Apr 21 '19 at 12:55
  • Is a thing like that ? https://www.getpostman.com/collections/ec0749a88736e112b87b Here you have my request , i dont know if its percebtible. – Afonso Lobo Apr 21 '19 at 13:58
  • I did that and failed to i dont know what it is – Afonso Lobo Apr 21 '19 at 14:35
  • Did you changed the content-type header to application/json? did you used the new JSON I added in my response? – Adi Ohana Apr 21 '19 at 14:57
  • Can you please share the stack trace from your server? – Adi Ohana Apr 21 '19 at 15:20
  • The server was down, it does not throws the error now, i tought i had changed the headres but i didnt't. Although, it also returns an empty JSON Object. – Afonso Lobo Apr 21 '19 at 16:00
  • if you are using jackson in your webapp (used by default in spring boot), then there is no mapping from JSONObject. you can return your response in Map instead of JSONObject. use this: https://stackoverflow.com/questions/44839753/returning-json-object-as-response-in-spring-boot for a reference – Adi Ohana Apr 21 '19 at 16:24
  • I'wil see it ! Really appreciate your help ! Thank You – Afonso Lobo Apr 21 '19 at 16:33