将JSON对象属性反序列化为字符串

将JSON对象属性反序列化为字符串

问题描述:

我有一些具有各种属性的JSON,其中大多数是简单数据类型.但是,我在JSON中有一个属性,当我将其反序列化为C#类时,我只需要将其反序列化为字符串即可.

I have some JSON that has a variety of properties most of which are simple data types. However, I have one property in the JSON that when I deserialize it to a C# class I simply need it to be deserialized as a string.

示例JSON:

{"simpleProperty": "value1", "json":{"a":"a1", "b":"b1"}}

"json"对象没有设置结构,只是它将是有效的JSON对象.

The "json" object has no set structure other than it will be a valid JSON object.

因此在上面的示例中,"json"的值是一个JSON对象-但是当它反序列化时,我需要将它作为字符串.

So in the above example the value of "json" is a JSON object -- but when it gets deserialized, I need it as a string.

所以,如果我的C#类是:

So if my C# class is:

public class MyClass
{
    public string SimpleProperty { get; set; }
    public string Json { get; set; }
}

然后,如果我使用:

var myClass = JsonConvert.DeserializeObject<MyClass>(jsonStr);

我希望myClass.Json只是一个简单的字符串.

I would like myClass.Json to just be a simple string.

我已经考虑过为此创建一个自定义JsonConverter,但是对于这种简单的东西来说似乎太复杂了.我一定在这里想念什么.任何方向将不胜感激.

I have looked at creating a custom JsonConverter for this but that seems way too complex for something this simple. I must be be missing something here. Any direction would be greatly appreciated.

我也看到了这篇文章-但它确实没有回答这个问题:

I also saw this post -- but it really doesn't answer the question: JSON.Net - How to deserialize JSON to object but treating a property as a string instead of JSON?

出于我的需要,我决定继续执行以下自定义JsonConverter:

For my needs, I decided to go ahead and implement a custom JsonConverter as follows:

    class JsonConverterObjectToString : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return (objectType == typeof(JTokenType));
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JToken token = JToken.Load(reader);
            if (token.Type == JTokenType.Object)
            {
                return token.ToString();
            }
            return null;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            //serializer.Serialize(writer, value);

            //serialize as actual JSON and not string data
            var token = JToken.Parse(value.ToString());
            writer.WriteToken(token.CreateReader());

        }
    }

我还没有对上述实现进行彻底的测试,并且我对Canconvert方法并不完全确定,因为它似乎从未被调用过,但是它似乎进行了转换,然后允许我将反序列化的类存储到MongoDB中, JSON数据正在存储一个字符串.所以目前一切都很好.我发现以下对实施有帮助:

I have not thoroughly tested the above implementation and I am not entirely sure about the Canconvert method as it never seemed to get invoked, but it seems to do the conversion and then allows me to then store the deserialized class into MongoDB and the JSON data is being stored a string. So all is good for now. I found the following helpful in the implementation: How to deserialize a JSON property that can be two different data types using Json.NET

更新:修改了WriteJson方法,以序列化回JSON对象(不是字符串).

UPDATE: modified WriteJson method to serialize back out as JSON object (not a string).