json字符串的部分序列化:只使用JSON.NET的顶级

时间:2014-06-12 23:15:38

标签: c# json serialization json.net

请为我澄清一些困惑:我有一个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会返回一个字符串,其中的所有内容都在&#34;信号&#34;之前&#34;选项&#34;,但它失败了,因为它试图前进并序列化它下面的所有内容。我得到了JsonReaderException

  

读取字符串时出错。意外的令牌:StartArray。路径&#39;信号&#39;,第1行,第12位。

我尝试使用JsonSerializerSettings(比如将maxDepth设置为1),但没有任何帮助。任何人吗?

我的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"
}

1 个答案:

答案 0 :(得分:3)

正如您所推测的那样,您收到此错误,因为您的字段是字符串并且反序列化程序找到了一个数组。它不能将数组分配给字符串。

您可以通过将自定义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]属性装饰您希望视为“blob”的字符串属性,如下所示:

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