反序列化在C#中不同类型的对象多JSON阵列 [英] Deserializing multiple JSON-arrays of different types of objects in C#

查看:179
本文介绍了反序列化在C#中不同类型的对象多JSON阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON字符串有固定数量的不同对象的数组(在Java中创建与一个JSONObjects JSONArrays

I have a JSON-string with a fixed number of arrays of different objects (created in Java with JSONObjects and JSONArrays):

[
  [ //The first type of object
    {
      "Initials":"MUS"
    },
    {
      "Initials":"NA"
    }
  ],
  [ //The second type
    {
      "ToolId":17
    },
    {
      ...
    }
  ]
  ... //etc.
]

所以,我创建了具有相应的阵列,其工作范围内的属性的对象一些虚拟类:

So I've created some Dummy-classes that has corresponding properties to the objects within the array, which works:

private class DummyEmployee
{
    public string Initials { get; set; }
}
//etc.

但我无法弄清楚如何容器类应设计。这是我做的:

But I can't figure out how the container class should be designed. This is how I did it:

private class DataContainer
{
    public List<DummyEmployee> Employees { get; set; }
    public List<DummySecondType> SecondTypes { get; set; }
    //etc.
}

这是我尝试反序列化JSON数据:

This is how I attempt to deserialize the JSON-data:

JavaScriptSerializer ser = new JavaScriptSerializer();

string jsonDataFromClient = ...;

DataContainer jsonData = ser.Deserialize<DataContainer>(jsonDataFromClient);

和它不工作。我收到以下错误而通过数据:
键入GUI.ValidateLoginData + DataContainer'不支持数组的反序列化。

And it doesn't work. I get the following error while passing the data: Type 'GUI.ValidateLoginData+DataContainer' is not supported for deserialization of an array.

我找不到任何其他学科上不同的反序列化对象数组的问题。

I couldn't find any other subjects on the matter of deserializing arrays of different objects.

推荐答案

是的,我将无法正常工作结果
注意到,在你的JavaScript对象基本上是一个数组,因为JavaScript是一种动态语言有它是一个有效的数组结果,其中如C#心不是这样一个数组(或列表)必须包含相同类型的对象。但是如果你仍然需要实现这一点,你可以控制你的JSON结构,它的编辑本

yes i will not work
notice that in your javascript object is basically an array and because javascript is a dynamic language there it is a valid array
where as c# isnt so an array(or list) must contain objects of same kind. however if you still need to implement this and you have control over your JSON structure edit it to this

{
  Employees:[ //The first type of object
    {
      "Initials":"MUS"
    },
    {
      "Initials":"NA"
    }
  ],
  SecondTypes:[ //The second type
    {
      "ToolId":17
    },
    {
      ...
    }
  ]
  ... //etc.
}

和当前的C#对象可能正确映射。搜索结果
如果你没有在JSON结构控制,那么你必须使用C#结果动态对象
更新: - 在其中你没有控制自己的JSON结构(或你不想要编辑)的情况下结果
尝试反序列化的JSON对象动态类型结果的数组
更新2: - ,因为你是好奇尝试反序列化现有的JSON结构类型的对象列表&LT;名单&LT;动态&GT;&GT; 虽然我还没有尝试过,但它应该工作正常。结果
然而,这种解决方案的一个缺点是,你将无法两种不同类型的对象来区分,即员工 SecondTypes

and your current c# object might map correctly.

and if you dont have control over the JSON structure then you have to use dynamic objects in c#
UPDATE:-for the case in which you dont have control over your JSON structure (or you dont wanna edit it).
try deserializing your JSON object to an array of dynamic type
UPDATE 2:- because you are curious try deserializing the existing JSON structure to an object of type List<List<dynamic>> and though i havent tried it but it should work fine.
one disadvantage of this solution however is that you wont be able to distinguish between two different types of objects namely Employee and SecondTypes

这篇关于反序列化在C#中不同类型的对象多JSON阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆