【问题标题】:String to Json converter is giving invalid Json value字符串到 Json 转换器给出了无效的 Json 值
【发布时间】:2017-04-04 22:39:54
【问题描述】:

嗨,我的 Json 请求中有一个字段,因为它的值是 Json。所以我将字符串转换为 Json 应用转义字符。这个转换后的字符串再次通过 Object 到 Json 转换器,并将其转换为无效的 Json。

我需要什么:

"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\":\"ref\"}"

我想得到什么:

"attributes" : {"type" : "CARES_Diagnosis_Survey__c", "referenceId" : "ref1"}

我尝试在此字段上使用 JSonIgnore 来跳过序列化,但如果我这样做了,它将完全忽略该字段。 请帮帮我。

【问题讨论】:

    标签: java json string spring-integration


    【解决方案1】:

    您可能做错了,您没有将字符串映射到 JSON,而是将对象映射到 JSON。

    因此,如果您希望将 salesForceDiagnosisObject 序列化为包含如下属性的 JSON:

    { 
      "key1" : "value1",
      "key2" : "value2",
    ....
      "attributes" : {
        "type" : "CARES_Diagnosis_Survey__c", 
        "referenceId" : "ref1"
    } 
    

    你的 salesForceDiagnosisObject 类不能是:

    class SalesForceDiagnosisObject {
      String key1;
      String key2;
      String attributes;
       ....
    }
    

    一定是这样的:

    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);
    

    【讨论】:

    • 非常感谢@minus。它完美地解决了。是的,我做错了。我以错误的方式看到了我的请求。我一直认为这是一个单一的领域,但事实并非如此。谢谢
    【解决方案2】:

    您的字符串看起来是正确的,它是 只是 反斜杠(用于转义双引号)使字符串看起来有点奇怪。但是,如果您 sysout 生成的字符串,则它不包含斜杠。

    以下是使用ObjectMapperattributes 转换为Map 的示例代码:

    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);
    }
    

    【讨论】:

    • 是的,当我调试时,在通过我的 spring 集成中配置的 Object to Json 转换器之前,我可以看到没有斜线的正确字符串。我相信它再次将这个字符串转换为 Json 并给出这个不正确的 Json 。我尝试了 Object mapper 并使用 mappervalue.toString 在模型中进行设置。它被设置为 {type=CARES_Diagnosis_Survey__c, referenceId=ref} 并且最终输出看起来像“属性”:“{type=CARES_Diagnosis_Survey__c, referenceId=ref}”,所以这也是作为类型失败的,它的值没有有双重要求
    • 也谢谢你@Darshan
    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多