I need to transfer from server to client 2 array

return ResponseEntity.status(HttpStatus.OK).body(new Gson().toJson( new Gson().toJson(calendarService.createMonthlyEvents(monthlyEventDto)) + new Gson().toJson(calendarService.getDaysWerentCreated()))); 

I tried to create an object on the server, assign each name to the corresponding array:

 new Gson.toJson("{ daysWhenCreated:" + new Gson().toJson(calendarService.createMonthlyEvents(monthlyEventDto))) + ", daysWhenNotCreated: " + new Gson().toJson(calendarService.getDaysWerentCreated() + "}"); 

A json type string comes to the client, I try to get a 1 result.daysWhenCreated from there, but I get undefined .

Tell me, please, what could be the problem?

Update

json output line is as follows:

 "{ [{"id":879,"name":"hhnefrd","startTime":"2016-12-‌​31T11:18:00","endT‌​ime":"2016-12-31T1‌​2:18:00","roomId"‌​:0,"description":​ "","recurrentId":‌​22,"color":"#1ba1‌​e2"}], ["30/1","3‌​1/1"] }" 
  • Show the line json - Barmaley
  • "{[{\" id \ ": 879, \" name \ ": \" hhnefrd \ ", \" startTime \ ": \" 2016-12-31T11: 18: 00 \ ", \" endTime \ ": \ "2016-12-31T12: 18: 00 \", \ "roomId \": 0, \ "description \": \ "\", \ "recurrentId \": 22, \ "color \": \ "# 1ba1e2 \ "}], [\" 30/1 \ ", \" 31/1 \ "]}" - Ihor

1 answer 1

Try this:

  HashMap<String,ВашКласс> content = new HashMap<String,ВашКласс>(); content.put("daysWhenCreated",calendarService.createMonthlyEvents(monthlyEventDto)); content.put("daysWhenNotCreated",calendarService.getDaysWerentCreated()); return ResponseEntity.status(HttpStatus.OK).body(new Gson().toJson(content)); 

Must be true.

Replace your ВашКласс with the return type in methods (If it is the same) or with Object if they are different.


A little bit about what is presented in your question:

  • In the first example, you add two lines with JSON and the result looks like this: "{...}{...}" , which is not a valid JSON
  • In the second variant, you are a string that is a valid JSON, repeatedly trying to bring it to the same form.
  • The JSON string that you specified does not match either the first or second option (since in the first case it is not valid, and the second must contain keys: daysWhenCreated and daysWhenNotCreated)
  • @Ihor, not at all ;-) If there are problems - write in the comments. - Mikhail Rebrov