7

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" } ]

1
  • Maybe you want to explain to us what you think JSON is. For me it is a string-based way of formatting data. Like the thing that you posted as "output". Commented Jul 14, 2016 at 12:24

5 Answers 5

13

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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.

Comments

1

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>

Comments

1

Add this to your controller method:

import org.springframework.http.MediaType;

@GetMapping(
    value = "/yourMapping", 
    produces = MediaType.APPLICATION_JSON_VALUE
)
public String yourControllerMethod(... ... ...) {
    ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.