0

I would like to create a JSONObject :

@RequestMapping(value = "/test", method = RequestMethod.GET)
@ResponseBody 
  public JSONObject  Test() {

 JSONObject test = new JSONObject();
 test.put("name","caroline");
 return test;
}

it's giving me as a result :

{"map":{"name":"caroline"}} 

But I was waiting for something like that :

{"name":"caroline"}

I don't know where is it the problem , I just followed this exemple

L_J
  • 2,351
  • 10
  • 23
  • 28
work
  • 27
  • 4
  • 12

4 Answers4

2

I tried with your code with a sample spring boot project and I get the error,

No converter for [class org.json.JSONObject]

The reason for this error is explained clearly here. To reiterate the answer, JSONObject classes don't have getters and hence the error. By default spring-boot starter web dependency has Jackson web support which can convert any POJO class to JSON object. So as the answer by @süleyman-can using a POJO is the right way to handle this.

In case, you can't use a POJO class because the fields in the response will be different for each request. For example, you have to send

{"a": "b"}

for one response and

{"c": "d"}

for another response, you can always use Map<String, String> like this,

@RequestMapping(value = "/test", method = RequestMethod.GET)
@ResponseBody
public Map<String, String> test() {
  Map<String, String> test = new HashMap<>();
  test.put("name","caroline");
  return test;
}

and the response would come like this,

{"name":"caroline"}                                                                                                                      
Malathi
  • 2,119
  • 15
  • 40
  • you can find here my real problem , can you juste take a look : https://gis.stackexchange.com/questions/394460/how-to-generate-geojson-on-spring-boot – work Apr 22 '21 at 15:45
  • I like this. This is veery smart. A nice workaround – Gledi May 29 '23 at 23:03
1

I hope you are talking about org.json package

If you really want to use JSONObject to create your JSON, then the following code works. It's just that you can change the return type from JSONObject to String.

@RequestMapping(value = "/test", method = RequestMethod.GET)
@ResponseBody
public String  Test() {
    JSONObject test = new JSONObject();
    test.put("name","caroline");
    return test.toString();
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
kChak
  • 73
  • 9
  • Ya i'm using org.json package , you can find here my real question , https://gis.stackexchange.com/questions/394460/how-to-generate-geojson-on-spring-boot – work Apr 22 '21 at 15:36
  • if i do "test.toString();" in me case my geojson will be not correct , it's will give me something that not looke like a geojson – work Apr 22 '21 at 15:38
1

you can try this

1- add this dependecy in pom.xml

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.1</version>
</dependency>

2- i have this class for example

public class Car {

private String color;
private String type;

// standard getters setters

}

2- Java Object to JSON

ObjectMapper objectMapper = new ObjectMapper();
Car car = new Car("yellow", "renault");
objectMapper.writeValue(new File("target/car.json"), car);

must result like it:

{"color":"yellow","type":"renault"}

3- JSON to Java Object

String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
Car car = objectMapper.readValue(json, Car.class);
  • you can find here my real problem , can you juste take a look : https://gis.stackexchange.com/questions/394460/how-to-generate-geojson-on-spring-boot – work Apr 22 '21 at 15:45
  • I am faced a problem like this, but it is not on the same topic, in fact, that I worked to solve it through replace function if not find solved your issues you can try this way good luck @work – Abdalrhman Alkraien Apr 22 '21 at 21:51
0

You can do by creating a Class

class MyResponseClass {

  private String name;
    
  public MyResponseClass(String name) {
    this.name = name;
  }
    
  public String getName() {
    return name;
  }
    
  public void setName(String name) {
    this.name = name;
  }
}

Initial:


@RestController
class MyController {

   @GetMapping("/test")
   public MyResponseClass getMyResponseClass() {
   final MyResponseClass test = new MyResponseClass("caroline");
   return test;
   }
}

I suggest you read this article: Building an Application with Spring Boot

Süleyman Can
  • 353
  • 3
  • 11
  • you can find here my real problem , you can you juste take a look : https://gis.stackexchange.com/questions/394460/how-to-generate-geojson-on-spring-boot – work Apr 22 '21 at 15:45