简体   繁体   English

字符串反序列化并在Java中转换为JSON

[英]String deserialization and conversion to JSON in Java

After deserializing my string and converting it to JSON using the code below: 在反序列化我的字符串并将其使用以下代码转换为JSON之后:

JSONObject returnValue = new JSONObject();
String toJson = null;

    try
    {
        Object otherObjectValue = SerializationUtils
                .deserialize(myBytesArray);
        Gson gson = new Gson();
        toJson = gson.toJson(otherObjectValue);
        returnValue.put(key, toJson);
    }

some part of the JSON still has something like: JSON的某些部分仍具有类似内容:

{ "key":"ATTRIBUTE_LIST", "value":"{\\"attributeContract\\":[{\\"scope\\":\\"sso\\",\\"name\\":\\"SAML_SUBJECT\\",\\"description\\":\\"Click to Edit\\",\\"required\\":true}]}"}

which means everything in: 这意味着:

"{\\"attributeContract\\":[{\\"scope\\":\\"sso\\",\\"name\\":\\"SAML_SUBJECT\\",\\"description\\":\\"Click to Edit\\",\\"required\\":true}]}"

is one string instead being another object with fields. 是一个字符串,而不是另一个具有字段的对象。 Is there something I can do to sanitize by JSONObject to make it properly JSON? 我可以做些什么来通过JSONObject进行清理以使其正确地成为JSON吗?

The key part is OK, means the whole String is JSON formatted. key部分是确定的,这意味着整个String都是JSON格式的。 For the value part, / shows that the value of value is JSON formatted already. 对于value部分, /表示value的value已经为JSON格式。

So you may "deserialize" the value of value again to retrieve an Object result. 因此,您可以再次“反序列化” value的value以检索Object结果。 Or you may ask the creator of origin JSON, to serialize origin Object one time into JSON format. 或者,您可以要求原始JSON的创建者一次将原始Object序列化为JSON格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM