gpt4 book ai didi

c# - 反序列化动态 JSON 文件 C# NewtonSoft.JSON

转载 作者:行者123 更新时间:2023-11-30 15:28:21 27 4
gpt4 key购买 nike

正在对可能包含 2 个独立类的动态 JSON 文件进行反序列化,我不知道数组中将包含哪种类型的数据。

问题是,我将根对象反序列化为“Base”类型,“subtests”对象被反序列化为“Subtest”,但“subtests”数组可能是“Base”类型或“Subtest”类型。

问题:我将如何以编程方式确定如果对象包含“subtest”,我反序列化为 Base,如果不包含,它应该反序列化为“Subtest”?

非常感谢您在这方面的帮助,因为我的时间很紧。

(编辑:添加注释以显示每个对象应反序列化为什么类型)这是一个示例(JSON 数据):

{
// Deserializes to type "Base"
"host": "123456",
"last_time": "2014-09-15 07:04:49.205000",
"name": "myName",
"result": "FAIL",
"serial": "12345",
"start_time": "2014-09-15 06:53:36.976000",
// Deserializes to type "List<Subtest>"
"subtests": [
{
"data": {
"moredata": {
"ver": "123",
"real": 123
}
},
"description": "Description of Data",
"host": "123456",
"last_time": "2014-09-15 20:32:31.095000",
"name": "testname.py",
"result": "PASS",
"start_time": "2014-09-15 20:32:25.873000",
"version": "2.014.09.15"
},
{
// Nested within Subtest Array, Should deserialize to type "Base" again
"host": "123456",
"last_time": "2014-09-15 07:04:49.205000",
"name": "name of test suite",
"result": "FAIL",
"start_time": "2014-09-15 06:53:36.976000",
//Should deserialize to type "List<Subtest>"
"subtests": [
{
"description": "Description of Data",
"host": "123456",
"last_time": "2014-09-15 06:53:40.440000",
"name": "testname.py",
"result": "FAIL",
"start_time": "2014-09-15 06:53:36.976000",
"version": "2.014.09.15"
},
{
"description": "Test Description",
"host": "123456",
"last_time": "2014-09-15 06:54:34.905000",
"name": "testname.py",
"result": "PASS",
"start_time": "2014-09-15 06:54:34.827000",
"version": "2.014.09.15"
},
{
"host": "123456",
"last_time": "2014-09-15 06:55:01.156000",
"name": "testname.py",
"result": "FAIL",
"start_time": "2014-09-15 06:55:01.156000",
"version": "2.014.09.15"
},

],
"version": "1.45"
}
],
"version": "1.23"
}

示例(代码):

public class Base{
public string host { get; set; }
public DateTime last_time { get; set; }
public string name { get; set; }
public string result { get; set; }
public string serial { get; set; }
public DateTime start_time { get; set; }
public List<Subtest> subtests { get; set; }
public string version { get; set; }
}

public class Subtest {
[JsonProperty("data")]
public JObject Data { get; set; } // CHECK

[JsonProperty("description")]
public string Description { get; set; } // CHECK

[JsonProperty("host")]
public string Host { get; set; }

[JsonProperty("info")]
public List<StatusDetails> Info { get; set; }

[JsonProperty("last_time")]
public DateTime LastRunTime { get; set; }

[JsonProperty("name")]
public string TestName { get; set; }

[JsonProperty("result")]
public string SubtestRunResult { get; set; }

[JsonProperty("start_time")]
public DateTime StartTime { get; set; }

[JsonProperty("trace")]
public List<object> Trace { get; set; }

[JsonProperty("version")]
public string Version { get; set; }
}

最佳答案

我会修改您的类以形成层次结构。我可能在这里遗漏了一个属性,但你明白了。重要的是转换器。

public abstract class TestBase
{
public string Host { get; set; }

[JsonProperty("last_time")]
public DateTime LastTime { get; set; }

public string Name { get; set; }
public string Result { get; set; }

[JsonProperty("start_time")]
public DateTime StartTime { get; set; }
public string Version { get; set; }
}

public class TestSuite : TestBase
{
public string Serial { get; set; }
public List<TestBase> Subtests { get; set; }
}

public class Subtest : TestBase
{
public JObject Data { get; set; }

public string Description { get; set; }
}

然后,您需要一个自定义转换器来根据 subtests 属性的存在选择正确的类型:

public class TestBaseConverter : JsonConverter
{
public override object ReadJson(
JsonReader reader,
Type objectType,
object existingValue,
JsonSerializer serializer)
{
JObject obj = serializer.Deserialize<JObject>(reader);

TestBase result = null;

if (obj["subtests"] != null)
{
result = new TestSuite();
}
else
{
result = new Subtest();
}

serializer.Populate(obj.CreateReader(), result);

return result;
}

public override bool CanConvert(Type objectType)
{
return typeof(TestBase).IsAssignableFrom(objectType);
}

public override bool CanWrite
{
get { return false; }
}

public override void WriteJson(
JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotSupportedException();
}
}

然后你会像这样使用它:

TestSuite suite = JsonConvert.DeserializeObject<TestSuite>(
json, new TestBaseConverter());

关于c# - 反序列化动态 JSON 文件 C# NewtonSoft.JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25897199/

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