C#JSON Newtonsoft.JSon获取名称

时间:2015-12-21 21:00:52

标签: c# json json.net

我关注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"      
      }
    ]
  }
}

我跟随Car对象:

class Car
{
    public Car() { }

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

如果&#34; engines = null&#34;我需要处理大小写。如果它不为null,则获取所有引擎名称。因此,例如上面,我对本田和大众的EngineNames列表将是:

  • Honda.EngineNames = {&#34; 1.2L&#34;,&#34; 1.8L&#34;} //有2个名字
  • 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;
}

2 个答案:

答案 0 :(得分:4)

最好的方法是削减这种动态的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();
    }
}

答案 1 :(得分:1)

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

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