json字符串的部分序列化:仅在JSON.NET中采用顶层

本文关键字:NET JSON 仅在 字符串 序列化 json | 更新日期: 2023-09-27 18:08:19

请为我澄清一些困惑:我有一个JSON字符串,我试图粘贴到一个对象,但我只想在顶层,并将下面的一切视为blob或字符串。像这样。

这是我的模型:
public class InputJson
{
    [JsonProperty(PropertyName = "signals")]
    public string signals{ get; set; }
    [JsonProperty(PropertyName = "options")]
    public string options { get; set; }
    [JsonProperty(PropertyName = "fields")]
    public string fields{ get; set; }
    [JsonProperty(PropertyName = "lapse")]
    public string lapse{ get; set; }
}

,我正试图反序列化JSON字符串(见下文)到它,像这样:

InputJson a = JsonConvert.DeserializeObject<InputJson>(body);

我希望a.signals会返回一个字符串,在"信号"下面和"选项"之前的所有内容,但它失败了,因为它试图向前并序列化它下面的所有内容。我得到一个JsonReaderException:

读取字符串错误。意想不到的标记:StartArray。路径"信号",1号线,位置12。

我试着玩JsonSerializerSettings(如设置maxDepth为1),但没有任何帮助。有人知道吗?

my JSON string:

{
    "signals":
    [
        {
            "name":"1",
            "att1":"44",
            "att2":"0",
            "att3":"18",
            "size":10,
            "points":[-79,-29,-9,-23,27,-110,-39,-22,-32,-2]
        },
        {
            "name":"2",
            "att1":"46",
            "att2":"0",
            "att3":"12",
            "size":10,
            "points":[36,37,37,35,38,41,41,45,39,41]
        }
    ],
    "options":"opt1 opt2",
    "fields":"myfields",
    "lapse":"somelapse"
}

json字符串的部分序列化:仅在JSON.NET中采用顶层

正如您正确猜测的那样,您得到这个错误是因为您的字段是字符串,而反序列化器发现了一个数组。不能将数组赋值给字符串。

您可以通过使用自定义JsonConverter来解决此问题,您希望将其视为"blobs",我假设您的意思是您只想要原始JSON字符串。

下面是你需要的转换器代码:

class BlobJsonConverter: JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(string));
    }
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JToken token = JToken.Load(reader);
        return token.ToString(Formatting.None);
    }
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

然后,在您的模型中,用[JsonConverter]属性装饰那些您想要处理为"blobs"的字符串属性,如下所示:

public class InputJson
{
    [JsonProperty(PropertyName = "signals")]
    [JsonConverter(typeof(BlobJsonConverter))]
    public string signals { get; set; }
    [JsonProperty(PropertyName = "options")]
    public string options { get; set; }
    [JsonProperty(PropertyName = "fields")]
    public string fields { get; set; }
    [JsonProperty(PropertyName = "lapse")]
    public string lapse { get; set; }
}

现在当你反序列化时,signals将在"信号"下面包含原始JSON,而不是给出一个错误。

演示:

class Program
{
    static void Main(string[] args)
    {
        string json = @"
        {
            ""signals"":
            [
                {
                    ""name"":""1"",
                    ""att1"":""44"",
                    ""att2"":""0"",
                    ""att3"":""18"",
                    ""size"":10,
                    ""points"":[-79,-29,-9,-23,27,-110,-39,-22,-32,-2]
                },
                {
                    ""name"":""2"",
                    ""att1"":""46"",
                    ""att2"":""0"",
                    ""att3"":""12"",
                    ""size"":10,
                    ""points"":[36,37,37,35,38,41,41,45,39,41]
                }
            ],
            ""options"":""opt1 opt2"",
            ""fields"":""myfields"",
            ""lapse"":""somelapse""
        }";
        InputJson a = JsonConvert.DeserializeObject<InputJson>(json);
        Console.WriteLine("signals: " + a.signals);
        Console.WriteLine("options: " + a.options);
        Console.WriteLine("fields: " + a.fields);
        Console.WriteLine("lapse: " + a.lapse);
    }
}
输出:

signals: [{"name":"1","att1":"44","att2":"0","att3":"18","size":10,"points":[-79,-29,-9,-23,27,-110,-39,-22,-32,-2]},{"name":"2","att1":"46","att2":"0","att3":"12","size":10,"points":36,37,37,35,38,41,41,45,39,41]}]
options: opt1 opt2
fields: myfields
lapse: somelapse