C#JSON Newtonsoft.JSON获取名称

本文关键字:获取 JSON Newtonsoft C#JSON | 更新日期: 2023-09-27 18:27:35

我有以下JSon,并且我正在使用JSon.NET(Newtonsoft.JSon):

{
  "total_items": "62",
  "page_number": "6",
  "page_size": "10",
  "page_count": "7",
  "cars": {
    "car": [     
      {
        "car_name": "Honda",
        "engines": {
          "engine": [
            {
              "name": "1.2L"
            },
            {
              "name": "1.8L"
            }
          ]
        },
        "country": "Japan"
      },
      {
        "car_name": "Ford",
        "engines": {
          "engine": {
              "name": "2.2L"
          }
        },
        "country": "Japan"
      },
      { 
        "car_name": "VW",
        "engines": null,
        "country": "Germany"      
      }
    ]
  }
}

我有以下汽车对象:

class Car
{
    public Car() { }
    public string Name { get; set; }
    public string Country { get; set; }
    public List<String> EngineNames { get; set; }
}

如果"engines=null",我需要处理这种情况。如果它不为null,则获取所有引擎名称。所以,举个例子,我的本田和大众的发动机名称列表是:

  • Honda.EngineNames={"1.2L","1.8L"}//有两个名称
  • VW.EngineNames=null//由于没有提供引擎,因此没有任何内容

我需要解析上面的JSON来获取汽车数据。我正在解析car_name和country,但我不知道如何解析引擎数组中的所有引擎名称(数组可能为null)。

private Cars GetCars(string json)
{
    dynamic data = (JObject)JsonConvert.DeserializeObject(json);
    foreach (dynamic d in data.cars.car)
    {
        Car c = new Car(); 
        c.Name = (string)d.SelectToken("car_name");
        c.Country = (string)d.SelectToken("country");
        c.EngineNames = //HOW TO GET ALL ENGINE NAMES AND HANDLE NULL ?
        CarList.Add(c);
    }
    return CarList;
}

C#JSON Newtonsoft.JSON获取名称

最好的方法是去掉这种动态的a-la-javascript,只定义与JSON结构匹配的强类型模型:

public class Wrapper
{
    public Cars Cars { get; set; }
}
public class Cars
{
    public Car[] Car { get; set; }
}
public class Car
{
    [JsonProperty(PropertyName = "car_name")]
    public string Name { get; set; }
    public string Country { get; set; }
    public Engines Engines { get; set; }
}
public class Engines
{
    public Engines()
    {
        Engine = new Engine[0];
    }
    // We need to use a custom JSON converter
    // because of this pretty broken schema that you have
    // in which the engine property can be array and a standard
    // object at the same time
    [JsonConverter(typeof(EnginesConverter))]
    public Engine[] Engine { get; set; }
}
public class Engine
{
    public string Name { get; set; }
}

然后让JSON.NET实现将这个字符串转换回强类型对象的魔术:

var wrapper = JsonConvert.DeserializeObject<Wrapper>(json);

现在您有了一个强类型结构,您可以使用LINQ:更容易地将其映射到所需的C#DTO

public class CarsDto
{
    public CarsDto()
    {
        Engines = new List<string>();
    }
    public string Name { get; set; }
    public string Country { get; set; }
    public List<string> Engines { get; set; }
}

然后:

var dto = wrapper.Cars.Car.Select(c => new CarsDto
{
    Name = c.Name,
    Country = c.Country,
    Engines = (c.Engines ?? new Engines()).Engine.Select(e => e.Name).ToList(),
}).ToList();

最后是我们使用的自定义JSON转换器:

public class EnginesConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return true;
    }
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.StartArray)
        {
            return serializer.Deserialize<Engine[]>(reader);
        }
        else
        {
            Engine e = serializer.Deserialize<Engine>(reader);
            return new[] { e };
        }
    }
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

为什么不继续使用动态类型,并将对象属性作为动态访问,这样你就可以做到:

var car = new Car();
car.Name = (string)d.car_name;
car.EngineNames = (d.engines != null ? ((IEnumerable)d.engines).Cast<dynamic>().Select(e => (string)e.name) : null);