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

时间:2013-12-03 12:33:26

标签: c# arrays vb.net serialization 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#示例。)

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

更新

这是另一个使用对象数组映射您拥有的数组数组的示例。然后我提取了数组中的每一段数据。

希望这对您有所帮助:

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

答案 1 :(得分:0)

再次感谢您的努力!我已将您的示例代码翻译为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-each循环中的类型转换列表;我的数据对象有大约110个属性,其中两个具有子对象,每个属性有10个属性。在我的项目中还有3或4个其他数据对象,其大小最小。

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

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