如何在JSON.NET中反序列化包含字符串和对象的数组



我正在尝试在Visual Basic中将一个特殊数组反序列化为对象。以下json数据由我使用的Web服务提供;我感兴趣的部分是"数据"属性;基本上它是一个字符串数组的数组,每个数组代表一个对象:

{"timestamp":1385984969075,
"data":
[
  [1590,null,null,null,0],
  [1020,"data a",null,null,0],
  [1025,"data b",null,null,0],
  ...
  [2756,"data c",null,
    [
      {"id":2,"name":"Tom","mail":"tom@test.te","f_id":6,"md":1},
      {"id":3,"name":"Carl","mail":"carl@test.te","f_id":6,"md":1}
     ]
  ,3],
  [1277,"data d",null,null,0],
  ...
  ]}

在本例中,每个数组的数据项4可以为null,也可以包含对象数组。

我想将数组反序列化为对象列表,但我无法使其工作。我搜索了很多类似的帖子,但到目前为止,我找不到任何有用的东西。

(我用Visual Basic.net编写,但欢迎使用C#示例。)

有人能帮忙吗?

更新:

这里是另一个使用对象数组来映射您所拥有的数组的例子。然后,我提取了数组中的每一条数据。

再次希望这将帮助你:

var list = new People
        {
            PersonList = new object[]
            {
              new object[]
             {
               "Test1", "Test2", "Test3", null, new object[]{new Person{Name="John", Age=21}}, 1
             },
              new object[]
             {
               "Test4", "Test5", "Test6", null, null, 2
             },
             new object[]
             {
               "Test17", "Test8", "Test9", null, new object[]{new Person{Name="Sara", Age=31}}, 3
             },
                new object[]
             {
               "Test10", "Test11", "Test12", null, null, 4
             },
             new object[]
             {
                 "Test13", "Test14", "Test15", null, new object[]{new Person{Name="John", Age=31}}, 5
             }
             }
        };
        string output = JsonConvert.SerializeObject(list);
        var objList = JsonConvert.DeserializeObject<People>(output);
        objectList = objList.PersonList;
        foreach(JArray objectItem in objectList)
        {
            var stringOne = (string)objectItem[0];
            var stringTwo = (string)objectItem[1];
            var stringthree = (string)objectItem[2];
            var nullObj = objectItem[3];
            foreach(var individual in objectItem[4])
            {
                JObject obj = (JObject)JToken.FromObject(individual);
                var person = obj.ToObject<Person>();
                string name = person.Name;
                int age = person.Age;
            }
            var num = (int)objectItem[5];
        }

JSON:

{
"PersonList": [
    [
        "Test1",
        "Test2",
        "Test3",
        null,
        [
            {
                "Name": "John",
                "Age": 21
            },
            {
                "Name": "Pete",
                "Age": 44
            }
        ],
        1
    ],
    [
        "Test4",
        "Test5",
        "Test6",
        null,
        null,
        2
    ],
    [
        "Test17",
        "Test8",
        "Test9",
        null,
        [
            {
                "Name": "Sara",
                "Age": 31
            }
        ],
        3
    ],
    [
        "Test10",
        "Test11",
        "Test12",
        null,
        null,
        4
    ],
    [
        "Test13",
        "Test14",
        "Test15",
        null,
        [
            {
                "Name": "John",
                "Age": 31
            }
        ],
        5
    ]
]

}

人员类别:

 public class Person
{
    public string Name { set; get; }
    public int Age { set; get; }
}

PeopleList类:

 public class People
{
    public object[] PersonList { set; get; }
}

再次感谢您的努力!我已经将您的示例代码翻译成VB.NET(请注意,定义了"list"对象的第一个块必须是Visual Studio中的一行;我刚刚对其进行了格式化,以便更好地进行概述):

Dim list = New People With {.PersonList = 
        New Object() { 
            New Object() {"Test1", "Test2", "Test3", Nothing, New Object() 
                   {New Person With {.Name = "John", .Age = 21}}, 
            1}, 
            New Object() {"Test4", "Test5", "Test6", Nothing, Nothing, 2}, 
            New Object() {"Test17", "Test8", "Test9", Nothing, New Object() 
                   {New Person With {.Name = "Sara", .Age = 31}}, 
            3}, 
            New Object() {"Test10", "Test11", "Test12", Nothing, Nothing, 4}, 
            New Object() {"Test13", "Test14", "Test15", Nothing, New Object() 
                   {New Person With {.Name = "John", .Age = 31}}, 
            5} 
        } 
    }
    Dim output As String = JsonConvert.SerializeObject(list)
    MessageBox.Show(output)
    Dim objList = JsonConvert.DeserializeObject(Of People)(output)
    Dim objectList As Object() = objList.PersonList
    For Each objectItem As JArray In objectList
        Dim stringOne As String = CStr(objectItem(0))
        Dim stringTwo As String = CStr(objectItem(1))
        Dim stringThree As String = CStr(objectItem(2))
        Dim nullObj As Object = objectItem(3)
        For Each individual As Object In objectItem(4)
            Dim obj As JObject = DirectCast(JToken.FromObject(individual), JObject)
            Dim person As Person = obj.ToObject(Of Person)()
            Dim name As String = person.Name
            Dim age As Integer = person.Age
        Next
        Dim num As Integer = CInt(objectItem(5))
    Next

基本上,这种方法就是我现在要做的。我只是觉得必须有一种更优雅的方式(无意冒犯!)使用JSON.NET。在我看来,最大的缺点是for每个循环中的类型转换列表;我的数据对象有大约110个属性,其中两个具有子对象,每个子对象有10个属性。此外,在我的项目中还有3或4个其他数据对象的大小是最小的。

因此,我的想法是编写一个函数,从目标对象派生属性类型,然后或多或少地自动进行类型转换。我有一个web服务调用的属性名称列表,因此此列表可用于查找与响应项相对应的属性名称。

无论如何,我会看看jArray和jObject,因为到目前为止我还没有使用过它们。

相关内容

  • 没有找到相关文章

最新更新