0

我目前正在为外部 REST API(我无法控制)编写 ac# 包装器库,它返回 JSON。

反序列化以下 JSON

{
    "countries": {
        "2": {
            "name": "Albania",
            "isoCode": "AL",
            "dialCode": "+355"
        },
        "3": {
            "name": "Algeria",
            "isoCode": "DZ",
            "dialCode": "+213"
        },
        "4": {
            "name": "American Samoa",
            "isoCode": "AS",
            "dialCode": "+1684"
        }
    }
}

我的库中有一个方法GetCountries,它执行以下操作:

public List<Country> GetCountries()
{
  string endpointUrl = GenerateEndPointUri(...)

  var countries = IssueApiGETRequest<CountriesWrapper>(endpointUrl);

  return countries.Countries.Select(x =>
  {
    x.Value.Id = x.Key;
    return x.Value;
  }).ToList();
}

IssueAPIGetRequest看起来像这样:

private T IssueApiGETRequest<T>(string endPointUrl)
{
  using (var handler = new HttpClientHandler())
  {
    handler.Credentials = ...;

    using (HttpClient client = new HttpClient(handler))
    {
      var response = client.GetAsync(endPointUrl).Result;

      if (response.IsSuccessStatusCode)
      {
        string json = response.Content.ReadAsStringAsync().Result;
        var result = JsonConvert.DeserializeObject<T>(json);

        return result;
      }
      else
      {
        switch (response.StatusCode)
        {
          case HttpStatusCode.BadRequest:
            throw new InvalidParameterException("Invalid parameters");
        }
        throw new Exception("Unable to process request");
      }
    }
  }
}

这允许我为外部 API 上的所有 GET 端点定义一个通用方法,并将它们序列化为我自己定义的类型。

最后,我定义了这些类实体:

  [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
  internal class CountriesWrapper
  {
    [JsonProperty(PropertyName = "countries")]
    public IDictionary<int, Country> Countries { get; set; }
  }

  [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
  public class Country
  {
    public int Id { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }

    [JsonProperty(PropertyName = "isoCode")]
    public string IsoCode { get; set; }

    [JsonProperty(PropertyName = "dialCode")]
    public string DialCode { get; set; }
  }

我对 GetCountries 方法不太满意,该方法必须重申从反序列化返回的 Dictionary 并拥有CountriesWrapper该类。

问题:我想知道我是否错过了一个技巧,或者是否有人可以建议一种更清洁的方式来解决这个问题。同时保持向外部 API 发出 GET 请求的通用方法。

4

1 回答 1

0

这可以使用类似于此答案的 JsonConverter 来完成

我认为json很奇怪,反正我实现了可以读取那种json的转换器。我不认为它实施得很好,但你可以改进它

class CountriesConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(List<Country>));
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var l = new List<Country>();
        dynamic expando = new ExpandoObject();
        var temp = expando as IDictionary<string, object>;
        if (reader.TokenType == JsonToken.StartObject)
        {
            var newCountry = true;
            while (reader.TokenType != JsonToken.EndObject)
            {
                if(newCountry)
                    reader.Read();
                if (reader.TokenType == JsonToken.PropertyName)
                {
                    if (reader.Value != null && reader.Value.ToString() != "countries")
                    {
                        if (!temp.ContainsKey("Id"))
                        {
                            newCountry = true;
                            int id = 0;
                            if (Int32.TryParse(reader.Value.ToString(), out id))
                                temp.Add("Id", id);
                        }
                        else
                        {
                            var propertyName = reader.Value.ToString();
                            reader.Read();
                            temp.Add(propertyName, reader.Value.ToString());
                        }

                    }
                }
                else if (reader.TokenType == JsonToken.EndObject)
                {
                    l.Add(Country.BuildCountry(expando));
                    temp.Clear();
                    reader.Read();
                    newCountry = false;
                }
            }
            reader.Read();
            while (reader.TokenType != JsonToken.EndObject)
            {
                reader.Read();
            }
        }

        return l;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        //ToDo here we can decide to write the json as 
        //if only has one attribute output as string if it has more output as list
    }
}

和国家类,它只是 de BuildCountry 方法是一样的

[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class Country
{
    public int Id { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }

    [JsonProperty(PropertyName = "isoCode")]
    public string IsoCode { get; set; }

    [JsonProperty(PropertyName = "dialCode")]
    public string DialCode { get; set; }

    internal static Country BuildCountry(dynamic expando)
    {
        return new Country
        {
            Id = expando.Id,
            Name = expando.name,
            IsoCode = expando.isoCode,
            DialCode = expando.dialCode
        };

    }
}
于 2013-01-29T13:41:25.797 回答