【问题标题】:Handling JSON single object and array处理 JSON 单个对象和数组
【发布时间】:2017-10-21 08:59:21
【问题描述】:

我正在使用 Newtonsoft.Json 处理一些返回给我的 JSON 数据。根据我的要求,我可以取回如下所示的内容:

{
"TotalRecords":2,
"Result":
    [
        {
        "Id":24379,
        "AccountName":"foo"
        },
        {
        "Id":37209,
        "AccountName":"bar"
        }
    ],
"ResponseCode":0,
"Status":"OK",
"Error":"None"
}

{
    "Result":
    {
        "Id":24379,
        "AccountName":"foo"
    },
    "ResponseCode":0,
    "Status":"OK",
    "Error":"None"
}

所以有时“结果”是一个结果数组,或者“结果”可能是单个响应。

我尝试使用来自How to handle both a single item and an array for the same property using JSON.net 的答案,但我仍然遇到错误。

特别是我得到了一个

Newtonsoft.json.jsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List'...

自定义转换器如下所示:

public class SingleOrArrayConverter<T> : JsonConverter
    {
        public override bool CanConvert(Type objecType)
        {
            return (objecType == typeof(List<T>));
        }

        public override object ReadJson(JsonReader reader, Type objecType, object existingValue,
            JsonSerializer serializer)
        {
            JToken token = JToken.Load(reader);
            if (token.Type == JTokenType.Array)
            {
                return token.ToObject<List<T>>();
            }
            return new List<T> { token.ToObject<T>() };
        }

        public override bool CanWrite
        {
            get { return false; }
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }

我的响应类看起来像

public class TestResponse
    {
        [JsonProperty("Result")]
        [JsonConverter(typeof(SingleOrArrayConverter<string>))]
        public List<DeserializedResult> Result { get; set; }
    }
public class DeserializedResult
    {
        public string Id { get; set; }
        public string AccountName { get; set; }
    }

最后我的请求看起来像

List<TestResponse> list = JsonConvert.DeserializeObject<List<TestResponse>>(response.Content);

【问题讨论】:

  • 你只有两种答案吗?
  • 他们只会遵循这两种格式中的一种,尽管内容显然会有所不同。有时“结果”会有多个字段,有时只有一个或两个,无论它返回单个对象还是最多 100 个对象的数组。
  • 现在我将尝试编写代码解决方案。

标签: c# arrays json json.net


【解决方案1】:

我认为,没有办法理解你应该得到哪种类型的响应。这就是为什么我建议检查手动类型的响应:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace TestConsoleApp
{
    public class Class1
    {

        public class Result
        {
            public int Id { get; set; }
            public string AccountName { get; set; }
        }

        public class ModelWithArray
        {
            public int TotalRecords { get; set; }
            public List<Result> Result { get; set; }
            public int ResponseCode { get; set; }
            public string Status { get; set; }
            public string Error { get; set; }
        }

        public class Result2
        {
            public int Id { get; set; }
            public string AccountName { get; set; }
        }

        public class ModelWithoutArray
        {
            public Result2 Result { get; set; }
            public int ResponseCode { get; set; }
            public string Status { get; set; }
            public string Error { get; set; }
        }

        public static void Main(params string[] args)
        {
            //string json = "{\"TotalRecords\":2,\"Result\":[{\"Id\":24379,\"AccountName\":\"foo\"},{\"Id\":37209,\"AccountName\":\"bar\"}], \"ResponseCode\":0,\"Status\":\"OK\",\"Error\":\"None\"}";
            string json = "{\"Result\":{\"Id\":24379,\"AccountName\":\"foo\"},\"ResponseCode\":0,\"Status\":\"OK\",\"Error\":\"None\"}";

            if (checkIsArray(json))
            {
                ModelWithArray data = JsonConver.DeserializeObject<ModelWithArray >(json);
            }else
            {
                ModelWithoutArray data = JsonConver.DeserializeObject<ModelWithoutArray>(json);
            }

        }

        static bool checkIsArray(string json)
        {

            Dictionary<string, object> desData = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);

            if (desData["Result"].GetType().Name.Contains("Array"))
            {
                return true;
            }
            else
            {
                return false;
            }

        }

    }
}

【讨论】:

  • i propose to check manualy type of response。您不必这样做,包newtonsoft.json 提供了避免这种情况的方法。
【解决方案2】:

您的代码很好,只是需要进行一些类型调整。

这一行

List<TestResponse> list = JsonConvert.DeserializeObject<List<TestResponse>>(response.Content);

需要这样,因为您的回复是object,而不是List

TestResponse list = JsonConvert.DeserializeObject<TestResponse>(response);

然后你的自定义反序列化器属性:

[JsonConverter(typeof(SingleOrArrayConverter<string>))]

需要成为:

[JsonConverter(typeof(SingleOrArrayConverter<DeserializedResult>))]

因为您的Result 对象不是stringstrings 的数组,它是DeserializedResults 或DeserializedResult 的数组。

【讨论】:

  • 我欠你一杯啤酒(或任何你喝的东西)。我在踢自己,这是为了让它工作的微小调整。一千谢谢!
  • 它拯救了我的一天 :)
  • 谢谢。这很有帮助。
猜你喜欢
  • 1970-01-01
  • 2019-03-27
  • 1970-01-01
  • 1970-01-01
  • 2019-03-20
  • 2015-11-14
  • 2021-07-31
  • 1970-01-01
  • 2021-12-10
相关资源
最近更新 更多