For example, I want to generate a json string for ng-style
:
例如,我想为ng样式生成一个json字符串:
<th ng-style="{width:247}" data-field="code">Code</th>
But with jackson, the result is:
但对杰克逊来说,结果是:
<th ng-style="{"width":247}" data-field="code">Code</th>
It's not easy to read.
读起来不容易。
So I want jackson to generate the json string with single quote or no quotes. Is it possible to do this?
因此,我希望jackson用单引号或无引号生成json字符串。有可能这样做吗?
2 个解决方案
#1
28
If you have control over the ObjectMapper
instance, then configure it to handle and generate JSON the way you want:
如果您对ObjectMapper实例有控制权,那么将其配置为按您希望的方式处理和生成JSON:
final ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
#2
-4
The simplest and the best option is to use regular expression and update the string value.
最简单和最好的选择是使用正则表达式并更新字符串值。
The sample code is as listed below.
示例代码如下所示。
partNumberList=partNumberList.replaceAll(":", ":\"").replaceAll("}", "\"}");
partNumberList = partNumberList。replaceAll(":",":\“”)。replaceAll(“}”,“\”}”);
The complete code is as shown below
完整的代码如下所示。
public static void main(String[] args) throws JsonParseException, JsonMappingException,
IOException {
TestJack obj = new TestJack();
//var jsonString ='{"it":"Stati Uniti d'America"}';
// jsonString =jsonString.replace("'", "\\\\u0027")
ObjectMapper mapper = new ObjectMapper();
String partNumberList = "[{productId:AS101R}, {productId:09902007}, {productId:09902002}, {productId:09902005}]";
partNumberList = partNumberList.replaceAll(":", ":\"").replaceAll("}", "\"}");
System.out.println(partNumberList);
mapper.configure(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
List<ProductDto> jsonToPersonList = null;
jsonToPersonList = mapper.readValue(partNumberList, new TypeReference<List<ProductDto>>() {
});
System.out.println(jsonToPersonList);
}
#1
28
If you have control over the ObjectMapper
instance, then configure it to handle and generate JSON the way you want:
如果您对ObjectMapper实例有控制权,那么将其配置为按您希望的方式处理和生成JSON:
final ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
#2
-4
The simplest and the best option is to use regular expression and update the string value.
最简单和最好的选择是使用正则表达式并更新字符串值。
The sample code is as listed below.
示例代码如下所示。
partNumberList=partNumberList.replaceAll(":", ":\"").replaceAll("}", "\"}");
partNumberList = partNumberList。replaceAll(":",":\“”)。replaceAll(“}”,“\”}”);
The complete code is as shown below
完整的代码如下所示。
public static void main(String[] args) throws JsonParseException, JsonMappingException,
IOException {
TestJack obj = new TestJack();
//var jsonString ='{"it":"Stati Uniti d'America"}';
// jsonString =jsonString.replace("'", "\\\\u0027")
ObjectMapper mapper = new ObjectMapper();
String partNumberList = "[{productId:AS101R}, {productId:09902007}, {productId:09902002}, {productId:09902005}]";
partNumberList = partNumberList.replaceAll(":", ":\"").replaceAll("}", "\"}");
System.out.println(partNumberList);
mapper.configure(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
List<ProductDto> jsonToPersonList = null;
jsonToPersonList = mapper.readValue(partNumberList, new TypeReference<List<ProductDto>>() {
});
System.out.println(jsonToPersonList);
}