Java 字符串到Json转换器提供的Json值无效
嗨,我的Json请求中有一个字段,因为它的值是Json。因此,我正在应用转义字符将字符串转换为Json。同样,这个转换后的字符串正在通过一个对象转换为Json转换器,并且转换后的字符串是无效的Json 我需要的是:Java 字符串到Json转换器提供的Json值无效,java,json,string,spring-integration,Java,Json,String,Spring Integration,嗨,我的Json请求中有一个字段,因为它的值是Json。因此,我正在应用转义字符将字符串转换为Json。同样,这个转换后的字符串正在通过一个对象转换为Json转换器,并且转换后的字符串是无效的Json 我需要的是: "attributes" : {"type" : "CARES_Diagnosis_Survey__c", "referenceId" : "ref1"}, 我在做什么: public static final String ATTRIBUTES_BEGIN = "{\"type\
"attributes" : {"type" : "CARES_Diagnosis_Survey__c", "referenceId" : "ref1"},
我在做什么:
public static final String ATTRIBUTES_BEGIN = "{\"type\"" +":"+ "\"CARES_Diagnosis_Survey__c\""+","+"\"referenceId\"" +":"+ "\"ref";
public static final String ATTRIBUTES_END = "\"}";
String attributes = ServiceConstants.ATTRIBUTES_BEGIN + ServiceConstants.ATTRIBUTES_END;
salesForceDiagnosisObject.setAttributes(attributes);
This is salesForceDiagnosisObject is going through Object to Json transformer in spring integration
<!-- turn to json -->
<int:object-to-json-transformer id="sfdcDiagnosisOutboundToJsonTransformer"
input-channel="sfdcDiagnosisObjectToJsonConverter"
output-channel="sfdcDiagnosisOutboundToJson"
/>
我想要的是:
"attributes" : {"type" : "CARES_Diagnosis_Survey__c", "referenceId" : "ref1"}
我曾尝试在这个字段上使用JSonIgnore跳过序列化,但如果我这样做了,它将完全忽略该字段。
请帮帮我。您的字符串看起来是正确的,只是后斜杠(用于转义双引号)使字符串看起来有点奇怪。但是,如果您
sysout
生成的字符串,它不包括斜杠
下面是使用ObjectMapper
将属性
转换为映射
的示例代码:
public static void main(String[] args) throws Exception{
String ATTRIBUTES_BEGIN = "{\"type\"" +":"+ "\"CARES_Diagnosis_Survey__c\""+","+"\"referenceId\"" +":"+ "\"ref";
String ATTRIBUTES_END = "\"}";
String attributes = ATTRIBUTES_BEGIN + ATTRIBUTES_END;
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> value = mapper.readValue(attributes, new TypeReference<Map<String,Object>>() {});
System.out.println(value);
}
publicstaticvoidmain(字符串[]args)引发异常{
字符串属性\u BEGIN=“{\”类型\+”:“+”\”关心\u诊断\ \u调查\ \u c \+”,“+”\“引用ID \+”:“+”\“引用”;
字符串属性\u END=“\”};
字符串属性=属性\u开始+属性\u结束;
ObjectMapper mapper=新的ObjectMapper();
Map value=mapper.readValue(属性,新类型引用(){});
系统输出打印项次(值);
}
您的字符串看起来是正确的,只是后斜杠(用于转义双引号)使字符串看起来有点奇怪。但是,如果您sysout
生成的字符串,它不包括斜杠
下面是使用ObjectMapper
将属性
转换为映射
的示例代码:
public static void main(String[] args) throws Exception{
String ATTRIBUTES_BEGIN = "{\"type\"" +":"+ "\"CARES_Diagnosis_Survey__c\""+","+"\"referenceId\"" +":"+ "\"ref";
String ATTRIBUTES_END = "\"}";
String attributes = ATTRIBUTES_BEGIN + ATTRIBUTES_END;
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> value = mapper.readValue(attributes, new TypeReference<Map<String,Object>>() {});
System.out.println(value);
}
publicstaticvoidmain(字符串[]args)引发异常{
字符串属性\u BEGIN=“{\”类型\+”:“+”\”关心\u诊断\ \u调查\ \u c \+”,“+”\“引用ID \+”:“+”\“引用”;
字符串属性\u END=“\”};
字符串属性=属性\u开始+属性\u结束;
ObjectMapper mapper=新的ObjectMapper();
Map value=mapper.readValue(属性,新类型引用(){});
系统输出打印项次(值);
}
您可能做得不对,您没有将字符串映射到JSON,而是将对象映射到JSON
因此,如果希望将salesForceDiagnosisObject序列化为包含以下属性的JSON:
{
"key1" : "value1",
"key2" : "value2",
....
"attributes" : {
"type" : "CARES_Diagnosis_Survey__c",
"referenceId" : "ref1"
}
class SalesForceDiagnosisObject {
String key1;
String key2;
DiagnosisAttribute attributes;
....
}
class DiagnosisAttribute{
String type;
String referenceId;
...
}
DiagnosisAttribute da = new DiagnosisAttribute();
da.setType("CARES_Diagnosis_Survey__c");
da.setReferenceId("ref1");
salesForceDiagnosisObject.setAttributes(da);
您的salesForceDiagnosisObject类不能是:
class SalesForceDiagnosisObject {
String key1;
String key2;
String attributes;
....
}
一定是这样的:
{
"key1" : "value1",
"key2" : "value2",
....
"attributes" : {
"type" : "CARES_Diagnosis_Survey__c",
"referenceId" : "ref1"
}
class SalesForceDiagnosisObject {
String key1;
String key2;
DiagnosisAttribute attributes;
....
}
class DiagnosisAttribute{
String type;
String referenceId;
...
}
DiagnosisAttribute da = new DiagnosisAttribute();
da.setType("CARES_Diagnosis_Survey__c");
da.setReferenceId("ref1");
salesForceDiagnosisObject.setAttributes(da);
您的属性应设置如下:
{
"key1" : "value1",
"key2" : "value2",
....
"attributes" : {
"type" : "CARES_Diagnosis_Survey__c",
"referenceId" : "ref1"
}
class SalesForceDiagnosisObject {
String key1;
String key2;
DiagnosisAttribute attributes;
....
}
class DiagnosisAttribute{
String type;
String referenceId;
...
}
DiagnosisAttribute da = new DiagnosisAttribute();
da.setType("CARES_Diagnosis_Survey__c");
da.setReferenceId("ref1");
salesForceDiagnosisObject.setAttributes(da);
您可能做得不对,您没有将字符串映射到JSON,而是将对象映射到JSON 因此,如果希望将salesForceDiagnosisObject序列化为包含以下属性的JSON:
{
"key1" : "value1",
"key2" : "value2",
....
"attributes" : {
"type" : "CARES_Diagnosis_Survey__c",
"referenceId" : "ref1"
}
class SalesForceDiagnosisObject {
String key1;
String key2;
DiagnosisAttribute attributes;
....
}
class DiagnosisAttribute{
String type;
String referenceId;
...
}
DiagnosisAttribute da = new DiagnosisAttribute();
da.setType("CARES_Diagnosis_Survey__c");
da.setReferenceId("ref1");
salesForceDiagnosisObject.setAttributes(da);
您的salesForceDiagnosisObject类不能是:
class SalesForceDiagnosisObject {
String key1;
String key2;
String attributes;
....
}
一定是这样的:
{
"key1" : "value1",
"key2" : "value2",
....
"attributes" : {
"type" : "CARES_Diagnosis_Survey__c",
"referenceId" : "ref1"
}
class SalesForceDiagnosisObject {
String key1;
String key2;
DiagnosisAttribute attributes;
....
}
class DiagnosisAttribute{
String type;
String referenceId;
...
}
DiagnosisAttribute da = new DiagnosisAttribute();
da.setType("CARES_Diagnosis_Survey__c");
da.setReferenceId("ref1");
salesForceDiagnosisObject.setAttributes(da);
您的属性应设置如下:
{
"key1" : "value1",
"key2" : "value2",
....
"attributes" : {
"type" : "CARES_Diagnosis_Survey__c",
"referenceId" : "ref1"
}
class SalesForceDiagnosisObject {
String key1;
String key2;
DiagnosisAttribute attributes;
....
}
class DiagnosisAttribute{
String type;
String referenceId;
...
}
DiagnosisAttribute da = new DiagnosisAttribute();
da.setType("CARES_Diagnosis_Survey__c");
da.setReferenceId("ref1");
salesForceDiagnosisObject.setAttributes(da);
是的,当我调试时,我可以在查看在spring集成中配置的Object to Json transformer之前不带斜杠地查看正确的字符串。我相信它再次将这个字符串转换为Json,并给出不正确的Json。我尝试了对象映射器,并使用mappervalue.toString在模型中进行设置。它被设置为{type=CARES\u Diagnosis\u Survey\u c,referenceId=ref},最后的结果看起来像“attributes”:“{type=CARES\u Diagnosis\u Survey\u c,referenceId=ref}”,所以这也是作为类型失败的,并且它的值没有双quetesThanks@DarshanYes,当我调试时,在查看spring集成中配置的Object to Json transformer之前,我可以看到没有斜杠的正确字符串。我相信它再次将这个字符串转换为Json,并给出不正确的Json。我尝试了对象映射器,并使用mappervalue.toString在模型中进行设置。它被设置为{type=CARES\u Diagnosis\u Survey\u c,referenceId=ref},最后的结果看起来像是“attributes”:“{type=CARES\u Diagnosis\u Survey\u c,referenceId=ref}”,所以这也是作为类型失败的,它的值没有双quetesThanks you@darshan谢谢你一顿@减号。这一切都很顺利。是的,我做错了。我把我的要求看错了。我一直认为这是一个单一的领域,但事实并非如此。谢谢你,谢谢你。这一切都很顺利。是的,我做错了。我把我的要求看错了。我一直认为这是一个单一的领域,但事实并非如此。谢谢