Java 创建Json字符串
我使用Jackson库生成json字符串,如下所示:Java 创建Json字符串,java,spring,Java,Spring,我使用Jackson库生成json字符串,如下所示: ObjectMapper objectMapper = new ObjectMapper(); String str = objectMapper.writeValueAsString(model); {"x" : "This is x", "y" : "This is y"} {'x' : 'This is x', 'y' : 'This is y'} ObjectMapper objectMapper = new ObjectMap
ObjectMapper objectMapper = new ObjectMapper();
String str = objectMapper.writeValueAsString(model);
{"x" : "This is x", "y" : "This is y"}
{'x' : 'This is x', 'y' : 'This is y'}
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
String str = objectMapper.writeValueAsString(model);
例如,这段被剪断的代码生成如下的somtheing:
ObjectMapper objectMapper = new ObjectMapper();
String str = objectMapper.writeValueAsString(model);
{"x" : "This is x", "y" : "This is y"}
{'x' : 'This is x', 'y' : 'This is y'}
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
String str = objectMapper.writeValueAsString(model);
但我想生成如下内容:
ObjectMapper objectMapper = new ObjectMapper();
String str = objectMapper.writeValueAsString(model);
{"x" : "This is x", "y" : "This is y"}
{'x' : 'This is x', 'y' : 'This is y'}
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
String str = objectMapper.writeValueAsString(model);
我的意思是如何将双引号字符串更改为单引号字符串。我尝试如下更改代码:
ObjectMapper objectMapper = new ObjectMapper();
String str = objectMapper.writeValueAsString(model);
{"x" : "This is x", "y" : "This is y"}
{'x' : 'This is x', 'y' : 'This is y'}
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
String str = objectMapper.writeValueAsString(model);
但是这个被剪掉的代码生成了第一个。
当然,我可以用replace方法处理这个问题,但我希望Jackson库能帮我解决这个问题。
如何处理此问题?按以下方式配置ObjectMapper:
final ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
//this may be what you need
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
按以下方式配置ObjectMapper:
final ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
//this may be what you need
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
试着调查一下格森。在格森看来是这样的。 房子我的房子=新房子; Gson Gson=新Gson; 字符串json=gson.toJsonmyHouse; 完成
试着调查一下格森。在格森看来是这样的。 房子我的房子=新房子; Gson Gson=新Gson; 字符串json=gson.toJsonmyHouse; 完成
正如我在评论中所说,这不是有效的JSON,逃避它没有任何意义。你应该用另一种方式处理它 您应该将该对象放在属性中 我想你想要像这样的东西 {myString:{\fake json\:\foo\} 相反,您应该: {myString:{fake json:foo}}
这应该是正确的处理方法。正如我在评论中所说的,这不是有效的JSON,逃避它没有任何意义。你应该用另一种方式处理它 您应该将该对象放在属性中 我想你想要像这样的东西 {myString:{\fake json\:\foo\} 相反,您应该: {myString:{fake json:foo}} 这应该是正确的处理方法。objectMapper.configureJsonParser.Feature.ALLOW\u SINGLE\u QUOTES,true;是关于允许在将JSON反序列化为对象时使用单引号,而不是根据需要将对象序列化为JSON 在序列化中,问题似乎与Jackson 1.X的默认序列化程序有关。下面是Jackson用来编写字符串值的代码。如您所见,双引号是硬编码的,因此在配置过程中不可更改:
@Override
public void writeString(String text)
throws IOException, JsonGenerationException
{
_verifyValueWrite("write text value");
if (text == null) {
_writeNull();
return;
}
if (_outputTail >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = '"'; // <----------------- opening quote
_writeString(text); // <----------------- string actual value
// And finally, closing quotes
if (_outputTail >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = '"'; // <----------------- closing quote
}
输出:
Received.: {"x":"ab\"c","y":"x\"y'z","z":15,"b":true}
Converted: {'x':'ab\"c','y':'x\"y\'z','z':15,'b':true}
Received.: {x:'ab"c',y:'x"y\'z',z:15,b:true}
或2:在每个字符串字段上使用自定义JsonSerializer
声明自定义序列化程序:
public class SingleQuoteStringSerializer extends JsonSerializer<String> {
@Override
public void serialize(String str, JsonGenerator jGen, SerializerProvider sP)
throws IOException, JsonProcessingException {
str = str.replace("'", "\\'"); // turns all ' into \'
jGen.writeRawValue("'" + str + "'"); // write surrounded by single quote
}
}
并照常进行QUOTE_FIELD_NAMES==false用于取消引用字段名:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);
String str = objectMapper.writeValueAsString(model);
System.out.println("Received.: "+str);
输出:
Received.: {"x":"ab\"c","y":"x\"y'z","z":15,"b":true}
Converted: {'x':'ab\"c','y':'x\"y\'z','z':15,'b':true}
Received.: {x:'ab"c',y:'x"y\'z',z:15,b:true}
注意:由于您似乎希望将JSON嵌入到另一个JSON中,最后一种方法可能还需要转义s,请参见x:'abc'。objectMapper.configureJsonParser.Feature.ALLOW_SINGLE_quote,true;是关于允许在将JSON反序列化为对象时使用单引号,而不是根据需要将对象序列化为JSON
在序列化中,问题似乎与Jackson 1.X的默认序列化程序有关。下面是Jackson用来编写字符串值的代码。如您所见,双引号是硬编码的,因此在配置过程中不可更改:
@Override
public void writeString(String text)
throws IOException, JsonGenerationException
{
_verifyValueWrite("write text value");
if (text == null) {
_writeNull();
return;
}
if (_outputTail >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = '"'; // <----------------- opening quote
_writeString(text); // <----------------- string actual value
// And finally, closing quotes
if (_outputTail >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = '"'; // <----------------- closing quote
}
输出:
Received.: {"x":"ab\"c","y":"x\"y'z","z":15,"b":true}
Converted: {'x':'ab\"c','y':'x\"y\'z','z':15,'b':true}
Received.: {x:'ab"c',y:'x"y\'z',z:15,b:true}
或2:在每个字符串字段上使用自定义JsonSerializer
声明自定义序列化程序:
public class SingleQuoteStringSerializer extends JsonSerializer<String> {
@Override
public void serialize(String str, JsonGenerator jGen, SerializerProvider sP)
throws IOException, JsonProcessingException {
str = str.replace("'", "\\'"); // turns all ' into \'
jGen.writeRawValue("'" + str + "'"); // write surrounded by single quote
}
}
并照常进行QUOTE_FIELD_NAMES==false用于取消引用字段名:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);
String str = objectMapper.writeValueAsString(model);
System.out.println("Received.: "+str);
输出:
Received.: {"x":"ab\"c","y":"x\"y'z","z":15,"b":true}
Converted: {'x':'ab\"c','y':'x\"y\'z','z':15,'b':true}
Received.: {x:'ab"c',y:'x"y\'z',z:15,b:true}
注意:由于您似乎希望将JSON嵌入到另一个JSON中,最后一种方法可能还需要转义s,请参见x:“abc”。我可以问您为什么吗?这不是一个有效的JSON。{x:this is x,y:this is y}代码放在另一个JSON剪接代码中,该代码以双代码开头并传递给客户端,我不想将反斜杠放在后面的其他双代码上,对于处理它,我想使用单代码而不是双代码。也许你应该用另一种方式处理它。如果你把它放在另一个字段里,它实际上是一个属性。我能问你为什么吗?这不是一个有效的JSON。{x:this is x,y:this is y}代码放在另一个JSON剪接代码中,该代码以双代码开头并传递给客户端,我不想将反斜杠放在后面的其他双代码上,对于处理它,我想使用单代码而不是双代码。也许你应该用另一种方式处理它。如果你在另一个字段中有这个,它实际上是一个属性。这个被剪断的代码生成{x:this is x,y:this is y}我想要单引号而不是双引号quote@Chris-我正在使用这两个mapper.configureJsonGenerator.Feature.QUOTE_字段_名称,false;mapper.configureJsonParser.Feature.ALLOW_UNQUOTED_FIELD_name,true;但是我还是得到了双引号,还有其他方法吗?这个截取的代码生成{x:这是x,y:这是y}我想要单引号而不是双引号quote@Chris-我正在使用这两个mapper.configureJsonGenerator.Feature.QUOTE_字段_名称,false;mapper.configureJsonParser.Feature.ALLOW_UNQUOTED_FIELD_name,true;但我还是得到了双引号,还有别的办法吗?