how to convert from string into json format in spring rest api

22,770

Solution 1

You can use Spring boot JSONObject

Example :

String content = "{"id":1,"name":"ram"}";
JSONObject jsonObject= new JSONObject(content );

After that you can return jsonObject from your spring controller.

dependency check the latest version from here:

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-configuration-processor</artifactId>
 <version>2.0.1.RELEASE</version>
</dependency>

Solution 2

I need in json format.

Short answer: Json format IS STRING.


Long one (explanation from wikipedia)

(JSON) is an open-standard format that uses human-readable text to transmit data objects consisting of attribute–value pairs. It is the most common data format used for asynchronous browser/server communication .....

As you can see, the String you get, has the correct attribte-value pairs format, so you can give it back to java object or you can store in a plain text file to get original java objects when needed


I need in this format: [ { "indexid": "425", "city": "Flin Flon" }, { "indexid": "220", "city": "London" }, { "indexid": "525", "city": "Longyear" } ]

If what you need is to have also numbers quoted, just change the type to String, you get in actual format because id is a numeric format, so no quotes are needed.

Solution 3

What you are trying to do is a json array and for that you can use the Gson library to transform an object to json.

try this:

Gson gson = new Gson();    
content = gson.toJson(list); //your list of Master_City

your result:

[{"indexid":65,"city":"Barcelona"},{"indexid":158,"city":"Dillons Bay"},{"indexid":232,"city":"East London"},{"indexid":411,"city":"Londonderry"},{"indexid":587,"city":"Thessaloniki"},{"indexid":818,"city":"Bouillon"},{"indexid":1719,"city":"Flin Flon"},{"indexid":2073,"city":"Clonmel"}]

dependency:

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.7</version>
</dependency>

Solution 4

In http request You have to set header with Content-Type = "application/json" then it will give response in json format

Solution 5

Add this to your controller method:

import org.springframework.http.MediaType;

@GetMapping(
    value = "/yourMapping", 
    produces = MediaType.APPLICATION_JSON_VALUE
)
public String yourControllerMethod(... ... ...) {
    ...
Share:
22,770
Naresh Gounder
Author by

Naresh Gounder

Updated on September 15, 2020

Comments

  • Naresh Gounder
    Naresh Gounder over 3 years

    this is the code:

    @RequestMapping(value="/find/city={city}", method=RequestMethod.GET)
    public @ResponseBody String getCity(@PathVariable String city) throws JsonParseException, IOException
    {      
      ObjectMapper mapper = new ObjectMapper();
      SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter.serializeAllExcept("id","miscellaneous","country","foundin","code","latlong","state");
      FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", theFilter);
      String content = "";
      StringBuilder builder = new StringBuilder();
      List<Master_City> list = City_Repository.findByCityLikeIgnoreCase(city);
     for (Master_City json : list)
      {
         builder.append( mapper.writer(filters).writeValueAsString(json));
        }
     content = builder.toString();
     return content;
    }
    

    output is not in json ,it's a string:

    {"indexid":65,"city":"Barcelona"}{"indexid":158,"city":"Dillons Bay"}     {"indexid":232,"city":"East London"}{"indexid":411,"city":"Londonderry"{"indexid":587,"city":"Thessaloniki"}{"indexid":818,"city":"Bouillon"}{"indexid":1719,"city":"Flin Flon"}{"indexid":2073,"city":"Clonmel"}
    

    I need in this format:

    [ { "indexid": "425", "city": "Flin Flon" }, { "indexid": "220", "city": "London" }, { "indexid": "525", "city": "Longyear" } ]