Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在SpringRESTAPI中将字符串转换为json格式_Java_Json_Spring_Mongodb_Rest - Fatal编程技术网

Java 如何在SpringRESTAPI中将字符串转换为json格式

Java 如何在SpringRESTAPI中将字符串转换为json格式,java,json,spring,mongodb,rest,Java,Json,Spring,Mongodb,Rest,代码如下: @RequestMapping(value="/find/city={city}", method=RequestMethod.GET) public @ResponseBody String getCity(@PathVariable String city) throws JsonParseException, IOException { ObjectMapper mapper = new ObjectMapper(); SimpleBeanPropertyF

代码如下:

@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;
}
我需要以下格式:

[ { “indexid”:“425”, “城市”:“弗林·弗隆” }, { “indexid”:“220”, “城市”:“伦敦” }, { “indexid”:“525”, “城市”:“龙年” } ]

我需要json格式

简短回答:Json格式是字符串。


长的一个(解释来自)

(JSON)是一种开放的标准格式,它使用人类可读文本来传输由属性-值对组成的数据对象。它是用于异步浏览器/服务器通信的最常见的数据格式

如您所见,您得到的字符串具有正确的attribte值对格式,因此您可以将其返回给java对象,或者在需要时存储在纯文本文件中以获取原始java对象


我需要以下格式: [{“indexid”:“425”,“城市”:“弗林弗隆”},{“indexid”:“220”,“城市”:“伦敦”},{“indexid”:“525”,“城市”:“隆年”}]

如果您还需要将数字加引号,只需将类型更改为String,就可以得到实际格式,因为id是数字格式,所以不需要加引号

我需要json格式

简短回答:Json格式是字符串。


长的一个(解释来自)

(JSON)是一种开放的标准格式,它使用人类可读文本来传输由属性-值对组成的数据对象。它是用于异步浏览器/服务器通信的最常见的数据格式

如您所见,您得到的字符串具有正确的attribte值对格式,因此您可以将其返回给java对象,或者在需要时存储在纯文本文件中以获取原始java对象


我需要以下格式: [{“indexid”:“425”,“城市”:“弗林弗隆”},{“indexid”:“220”,“城市”:“伦敦”},{“indexid”:“525”,“城市”:“隆年”}]


如果您需要将数字也加引号,只需将类型更改为字符串,就可以得到实际格式,因为id是数字格式,所以不需要引号。

您尝试做的是一个json数组,为此,您可以使用Gson库将对象转换为json

试试这个:

Gson gson = new Gson();    
content = gson.toJson(list); //your list of Master_City
您的结果:

[{"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"}]
依赖关系:

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

com.google.code.gson
格森
2.7

您试图做的是一个json数组,为此,您可以使用Gson库将对象转换为json

试试这个:

Gson gson = new Gson();    
content = gson.toJson(list); //your list of Master_City
您的结果:

[{"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"}]
依赖关系:

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

com.google.code.gson
格森
2.7

在http请求中,您必须使用Content Type=“application/json”设置标头,然后它将以json格式给出响应

在http请求中,您必须使用Content Type=“application/json”设置标头,然后它将以json格式给出响应

您可以使用Spring boot JSONObject

示例:

String content = "{"id":1,"name":"ram"}";
JSONObject jsonObject= new JSONObject(content );
之后,您可以从spring控制器返回jsonObject

从以下位置检查最新版本的相关性:


org.springframework.boot
spring引导配置处理器
2.0.1.1发布

您可以使用Spring boot JSONObject

示例:

String content = "{"id":1,"name":"ram"}";
JSONObject jsonObject= new JSONObject(content );
之后,您可以从spring控制器返回jsonObject

从以下位置检查最新版本的相关性:


org.springframework.boot
spring引导配置处理器
2.0.1.1发布

将此添加到控制器方法中:

import org.springframework.http.MediaType;

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

将此添加到控制器方法:

import org.springframework.http.MediaType;

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

也许你想向我们解释一下你认为JSON是什么。对我来说,它是一种基于字符串的格式化数据的方法。就像你发布的“输出”一样。也许你想向我们解释一下你认为JSON是什么。对我来说,它是一种基于字符串的格式化数据的方法。就像你发布的“输出”一样。