gpt4 book ai didi

C# 解析 JSON 问题

转载 作者:行者123 更新时间:2023-11-30 21:52:51 24 4
gpt4 key购买 nike

我有以下 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
}
]
}
}

我有以下 Car 对象:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com