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



正在反序列化一个动态JSON文件,该文件可能包含两个独立的类,我不知道数组中会有哪种类型的数据。

问题是,我将根对象反序列化为"Base"类型,将"subtests"对象反序列化至"Subtest",但"Subtest"数组的类型可以是"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());

我最终要做的是向Subtest类添加一个List<Subtest>属性,并在递归foreach循环函数中检查空值。虽然没有我希望的那么漂亮,但比解析它并单独反序列化每个subtest对象要好。

private static void GetSubtest(List<Subtest> subtestList) {
        foreach (var subtest in subtestList) {
            if (subtest.Subtests != null) {
                GetSubtest(subtest.Subtests);
            }
            else {
                // add data to Vertica cluster
            }
        }
    }

漫长的一天,真的很感激你们所有的帮助。JSON的新手,所以我无法理解它。希望这能在未来帮助其他人。如果你需要更多的解释,请在这里发表评论。

相关内容

  • 没有找到相关文章

最新更新