C# 解析 JSON 问题

标签 c# json

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

{
  "total_items": "62",
  "page_number": "6",
  "page_size": "10",
  "page_count": "7",
  "cars": {
    "car": [     
      {
        "car_name": "Honda", 
        "engines": {
          "engine": [  <-- HONDA has multiple engines, so this is an array
            {
              "name": "1.2L"
            },
            {
              "name": "1.8L"
            }
          ]
        },
        "country": "Japan"
        "image": {
            "thumb": {
                "url": "http://image_path/Honda.jpg" <-- Image provided
            }
        }
      },
      {
        "car_name": "Ford",
        "engines": {
          "engine": {   <-- FORD has single engine, so this is an object
              "name": "2.2L"
          }
        },
        "country": "Japan"
        "image": null  <-- image is null
      },
      { 
        "car_name": "VW",
        "engines": null,  <-- VW has no engines, so this is null
        "country": "Germany"  
        "image": null    <-- image is null
      }
    ]
  }
}

我有以下汽车对象:

class Car
{
    public Car() { }

    public string Name { get; set; }
    public string Country { get; set; }
    public List<String> EngineNames { get; set; }
}

我需要处理上述所有 3 种情况(HONDA 为数组,FORD 为对象,VW 为 null)。如果不为空,则获取所有引擎名称。因此,例如上面的例子,我的 3 辆车的 EngineNames 列表将是:

Honda.EngineNames = {"1.2L", "1.8L"} // array in JSON
Ford.EngineNames = {"2.2L"} //object in JSON
VW.EngineNames = null //null in JSON

我需要解析上面的 JSON 来获取汽车数据。我正在解析 car_name 和 Country,但我不知道如何通过处理上述 3 种情况来解析所有引擎名称。

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");

        // PROBLEM: This works fine for array or null in JSON above (HONDA and VW), but it errors on JSON object (in case of FORD)
        // When handling FORD, I get error "'Newtonsoft.Json.Linq.JProperty' does not contain a definition for 'name'"
        c.EngineNames = (d.engines != null ? ((IEnumerable)d.engines.engine).Cast<dynamic>().Select(e => (string)e.name) : null);

        CarList.Add(c);
    }
    return CarList;
}

最佳答案

使用 here 中的转换器(最初是提​​议的重复项,但这个问题的 JSON 还有一些其他问题)

你的类结构需要稍微修改一下。

查看此 JSON:

"cars": { <-- cars is an object, not an array
    "car": [ <-- the cars object actually contains the array
      {
        "car_name": "Honda", 
        "engines": { <-- same goes for this
          "engine": [
            {

因此,您需要编写包装类来正确反射(reflect) JSON。这是我想到的:

public class Root 
{
    public CarHolder Cars {get;set;}
}
public class CarHolder
{
    public IList<Car> Car { get; set; }
}
public class Car
{
    public Car() { }

    public string car_name { get; set; }
    public string Country { get; set; }

    public EngineHolder Engines { get; set; }
}
public class EngineHolder 
{
    [JsonConverter(typeof(SingleOrArrayConverter<Engine>))]
    public List<Engine> Engine { get; set; }
}
public class Engine
{
    public string Name { get; set; }
}

并使用上述问题的转换:

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

    public override object ReadJson(JsonReader reader, Type objectType, 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();
    }
}

用法:

var result = JsonConvert.DeserializeObject<Root>(jsonStr);
Console.WriteLine(result.Cars.Car[0].Engines.Engine[0].Name == "1.2L");
Console.WriteLine(result.Cars.Car[0].Engines.Engine[1].Name == "1.8L");
Console.WriteLine(result.Cars.Car[1].Engines.Engine[0].Name == "2.2L");
Console.WriteLine(result.Cars.Car[2].Engines == null);

全部打印true

循环汽车和引擎

foreach(var car in result.Cars.Car)
{
    if (car.Engines != null)
    {
        foreach(var engine in car.Engines.Engine)
        {
            var engineName = engine.Name;
        }
    }
}

关于C# 解析 JSON 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34405979/

相关文章:

c# - 创建成员(member)用户时出错 "The password-answer supplied is invalid"

C# static - MSDN 上的定义是什么意思?

c# - 如何使用反射在 C# 中获取自动属性的默认值?

iOS - API 对象和 Controller 之间的通信

json - 调用函数 'makeDecorator' ,不支持函数调用

c# - 我应该安装哪个版本的 VS 以允许在构建服务器上进行单元测试?

c# - 为扫雷游戏动态创建一个游戏板

python - 无法使用 python Minds api 进行身份验证

javascript - 让 JSON 与 Google Maps API 3 一起工作

javascript - 如何将ajax响应/结果放入变量php