3

我正在与返回 JSON 的 API 通信,该 JSON 包含 true、false 或字符串数​​组的数组。我希望反序列化此 JSON 并将布尔值(如果有)存储在数据类型 bool 的名为 Success 的类字段中,如果有,则将数组存储在自定义数据类型的名为 Result 的字段中。

实现这一目标的最佳方法是什么?

一些 JSON:

[
    {"status":"ok","result":true},
    {
        "status":"ok",
        "result":
        [
            {"name":"John Doe","UserIdentifier":"abc","MaxAccounts":"2"}
        ]
    }
]

我的结果类:

class Result
{
    string Name,
    string UserIdentifier,
    string MaxAccounts
}

与 Api 通信的类:

class Api
{
    public string Status { get; set; }
    public Result[] Result { get; set; }
    public bool Success { get; set; }
}
4

2 回答 2

4

使用 JSON.NET,您可以编写自定义 JSON 转换器。例如,您可以拥有以下对象:

public class Root
{
    public string Status { get; set; }
    public Result Result { get; set; }
}

public class Result
{
    public bool? Value { get; set; }
    public Item[] Items { get; set; }
}

public class Item
{
    public string Name { get; set; }
    public string UserIdentifier { get; set; }
    public string MaxAccounts { get; set; }
}

并且您的 JSON 将被反序列化为Root[].

以下是自定义 JSON 转换器的外观:

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

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Boolean)
        {
            return new Result
            {
                Value = (bool)reader.Value,
            };
        }

        return new Result
        {
            Items = serializer.Deserialize<Item[]>(reader),
        };
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        // If you want to support serializing you could implement this method as well
        throw new NotImplementedException();
    }
}

接着:

class Program
{
    static void Main()
    {
        var json = 
        @"[
            {
                ""status"": ""ok"",
                ""result"": true
            },
            {
                ""status"": ""ok"",
                ""result"": [
                    {
                        ""name"": ""John Doe"",
                        ""UserIdentifier"": ""abc"",
                        ""MaxAccounts"": ""2""
                    }
                ]
            }
        ]";

        var settings = new JsonSerializerSettings();
        settings.Converters.Add(new ResultConverter());
        Root[] root = JsonConvert.DeserializeObject<Root[]>(json, settings);
        // do something with the results here
    }
}
于 2013-07-29T12:17:14.773 回答
0

使用 Api 和 Results 对象创建和数组列表。我刚刚尝试过这个并且它有效。

        var api = new Api();
        var result = new Result();
        api.Status = "ok";
        api.Success = true;
        result.Name = "John Doe";
        result.UserIdentifier = "abc";
        result.MaxAccounts = "2";
        api.Result = new Result[1];
        api.Result[0] = result;
        var arrayList = new ArrayList() { new {api.Status, api.Success}, 
                        new { api.Status, api.Result} };
        var json = JsonConvert.SerializeObject(arrayList, Formatting.Indented);
于 2013-07-29T12:59:45.363 回答