将JSON字符串反序列化为C#列表

时间:2016-12-13 07:59:21

标签: c# json serialization

如何在C#中将JSON反序列化为List<SOROCat>,请帮助我。无法到达我错的地方。任何帮助都会非常感激。

JSON如下。

"[
  {
    "RequestOffering": "DC - Virtual Server Installation",
    "ServiceOffering": "DC - VPS",
    "CategoryGUID": "Enum.176787fee69b4feabae2ac0a02d1d7dc",
    "CategoryDisplayName": "Data Center",
    "TemplateName": "Template.a98f4f80e2a44454854fe314a3a98aab",
    "TemplateObject": {
      "TypeID": {
        "Name": null,
        "Id": "9dcf1f69-e495-af57-a30e-741af1836bd2",
        "Identifier": {
          "Value": "1|PishroSystem.ServiceRequest.Form/7a1fbae719247839|1.0.0.0|Afranet.ServiceRequest.Form_TypeProjection||"
        }
      },
      "TypeConstraint": null,
      "PropertyCollection": [],
      "ObjectCollection": [
        {
          "Path": "$Context/Path[Relationship='CustomSystem_WorkItem_Activity_Library!System.WorkItemContainsActivity' TypeConstraint='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity.ManualActivity']$",
          "PropertyCollection": [
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/SequenceId$",
              "MixedValue": "0"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/ChildId$",
              "MixedValue": "4171"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/Skip$",
              "MixedValue": "False"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Title$",
              "MixedValue": "انجام امور مالی قراردادی"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Description$",
              "MixedValue": "باسلام؛ احتراماً خواهشمند است پیرو امکان سنجی صورت گرفته و تایید آن توسط واحد فنی و با توجه به هماهنگی با نماینده مشتری اقدامات لازم در خصوص راه اندازی سرویس صورت گیرد. (اطلاعات مورد نیاز در بخش مربوطه در CRM موجود می‌باشد.)"
            }
          ],
          "ObjectCollection": []
        },
        {
          "Path": "$Context/Path[Relationship='CustomSystem_WorkItem_Activity_Library!System.WorkItemContainsActivity' TypeConstraint='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity.ManualActivity']$",
          "PropertyCollection": [
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/SequenceId$",
              "MixedValue": "5"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/ChildId$",
              "MixedValue": "4179"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/Stage$",
              "MixedValue": "4b667400-5448-3dd0-a4ee-2170a985375b"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/Skip$",
              "MixedValue": "False"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Title$",
              "MixedValue": "به روز رسانی سرورهای اتوماسیون"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Description$",
              "MixedValue": "باسلام؛ لطفاً سرورهای اتوماسیون را به روز رسانی کنید."
            }
          ],
          "ObjectCollection": []
        },
        {
          "Path": "$Context/Path[Relationship='CustomSystem_WorkItem_Activity_Library!System.WorkItemContainsActivity' TypeConstraint='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity.ManualActivity']$",
          "PropertyCollection": [
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/SequenceId$",
              "MixedValue": "1"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/ChildId$",
              "MixedValue": "4173"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/Stage$",
              "MixedValue": "4b667400-5448-3dd0-a4ee-2170a985375b"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/Skip$",
              "MixedValue": "False"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Title$",
              "MixedValue": "تخصیص سرور جهت راه اندازی سرویس - تیم VIRTUALIZATION"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Description$",
              "MixedValue": "باسلام؛ لطفاً تنظیمات فنی لازم جهت تخصیص سرویس vps به مشتری صورت گیرد."
            }
          ],
          "ObjectCollection": []
        },
        {
          "Path": "$Context/Path[Relationship='CustomSystem_WorkItem_Activity_Library!System.WorkItemContainsActivity' TypeConstraint='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity.ManualActivity']$",
          "PropertyCollection": [
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/SequenceId$",
              "MixedValue": "2"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/ChildId$",
              "MixedValue": "4175"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/Stage$",
              "MixedValue": "4b667400-5448-3dd0-a4ee-2170a985375b"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/Skip$",
              "MixedValue": "False"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Title$",
              "MixedValue": "انجام اقدامات تیم فنی جهت راه اندازی -  تیم VIRTUALIZATION"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Description$",
              "MixedValue": "باسلام؛ لطفاً اقدامات فنی لازم جهت تخصیص سرویس vps به مشتری صورت گیرد."
            }
          ],
          "ObjectCollection": []
        },
        {
          "Path": "$Context/Path[Relationship='CustomSystem_WorkItem_Activity_Library!System.WorkItemContainsActivity' TypeConstraint='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity.ManualActivity']$",
          "PropertyCollection": [
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/SequenceId$",
              "MixedValue": "4"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/ChildId$",
              "MixedValue": "4177"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/Stage$",
              "MixedValue": "4b667400-5448-3dd0-a4ee-2170a985375b"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/Skip$",
              "MixedValue": "False"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Title$",
              "MixedValue": "اطلاع رسانی دسترسی سرور به مشتری - تیم VIRTUALIZATION"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Description$",
              "MixedValue": "باسلام؛ لطفاً اطلاعات مربوط به دسترسی سرور به مشتری اطلاع راسنی شود."
            }
          ],
          "ObjectCollection": []
        },
        {
          "Path": "$Context/Path[Relationship='CustomSystem_WorkItem_Activity_Library!System.WorkItemContainsActivity' TypeConstraint='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity.ManualActivity']$",
          "PropertyCollection": [
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/SequenceId$",
              "MixedValue": "3"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/ChildId$",
              "MixedValue": "4185"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/Stage$",
              "MixedValue": "4b667400-5448-3dd0-a4ee-2170a985375b"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/Skip$",
              "MixedValue": "False"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Title$",
              "MixedValue": "فعال سازی FTP - فنی DC "
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Description$",
              "MixedValue": "باسلام؛ لطفاً با توجه به درخواست مشتری اقدامات لازم جهت فعال سازی FTP صورت گیرد."
            }
          ],
          "ObjectCollection": []
        },
        {
          "Path": "$Context/Path[Relationship='CustomSystem_WorkItem_Activity_Library!System.WorkItemContainsActivity' TypeConstraint='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity.ManualActivity']$",
          "PropertyCollection": [
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/SequenceId$",
              "MixedValue": "6"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/ChildId$",
              "MixedValue": "4189"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/Stage$",
              "MixedValue": "ddab38d4-30a9-3a4c-30dc-d207b954634a"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/Skip$",
              "MixedValue": "False"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Title$",
              "MixedValue": "به روزرسانی Ripe و Matma  - تیمCore"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Description$",
              "MixedValue": "باسلام؛ لطفاً اقدامات فنی لازم جهت به‌روزرسانی IP مشتری در Ripe و Matma صورت گیرد."
            }
          ],
          "ObjectCollection": [
            {
              "Path": "$Context/Path[Relationship='CustomSystem_WorkItem_Library!System.WorkItemAssignedToUser' TypeConstraint='CustomMicrosoft_Windows_Library!Microsoft.AD.Group']$",
              "PropertyCollection": [
                {
                  "Path": "$Context/Property[Type='CustomMicrosoft_Windows_Library!Microsoft.AD.UserBase']/DistinguishedName$",
                  "MixedValue": "CN=Core Team,OU=Users,OU=Members,OU=Core,OU=Afranet,DC=afranet,DC=local"
                },
                {
                  "Path": "$Context/Property[Type='CustomMicrosoft_Windows_Library!Microsoft.AD.UserBase']/ObjectGuid$",
                  "MixedValue": "86e05aeb-8ad5-4422-ae84-fa996d1015f0"
                },
                {
                  "Path": "$Context/Property[Type='CustomMicrosoft_Windows_Library!Microsoft.AD.UserBase']/SID$",
                  "MixedValue": "S-1-5-21-931204019-4140322259-2122501801-5360"
                },
                {
                  "Path": "$Context/Property[Type='CustomMicrosoft_Windows_Library!Microsoft.AD.UserBase']/FQDN$",
                  "MixedValue": "afranet.local"
                },
                {
                  "Path": "$Context/Property[Type='CustomMicrosoft_Windows_Library!Microsoft.AD.UserBase']/UPN$",
                  "MixedValue": "Core Team@afranet.local"
                },
                {
                  "Path": "$Context/Property[Type='CustomMicrosoft_Windows_Library!Microsoft.AD.UserBase']/OrganizationalUnit$",
                  "MixedValue": "Users,Members,Core,Afranet"
                },
                {
                  "Path": "$Context/Property[Type='CustomSystem_Library!System.Domain.User']/Domain$",
                  "MixedValue": "AFRANET"
                },
                {
                  "Path": "$Context/Property[Type='CustomSystem_Library!System.Domain.User']/UserName$",
                  "MixedValue": "Core Team"
                },
                {
                  "Path": "$Context/Property[Type='CustomSystem_Library!System.ConfigItem']/ObjectStatus$",
                  "MixedValue": "$MPElement[Name='CustomSystem_Library!System.ConfigItem.ObjectStatusEnum.Active']$"
                },
                {
                  "Path": "$Context/Property[Type='CustomSystem_Library!System.Entity']/DisplayName$",
                  "MixedValue": "AFRANET\\Core Team"
                }
              ],
              "ObjectCollection": []
            }
          ]
        }
      ],
      "XmlTag": "ObjectTemplate",
      "ManagementGroup": {
        "ConnectorFramework": {},
        "EntityTypes": {},
        "EntityObjects": {},
        "Knowledge": {},
        "LanguagePacks": {},
        "ManagementPacks": {},
        "Monitoring": {},
        "Overrides": {},
        "Presentation": {},
        "Dashboard": {},
        "Reporting": {},
        "Resources": {},
        "Security": {},
        "Subscription": {},
        "TaskConfiguration": {},
        "TaskRuntime": {},
        "Templates": {},
        "TypeCache": {},
        "TypeDefinitions": {},
        "DataWarehouse": {},
        "Notifications": {},
        "ServiceLevelAgreements": {},
        "Deployment": {},
        "TaskConfigurationInternal": {},
        "LinkingFramework": {},
        "UserSettings": {},
        "Name": "AfraSCSM",
        "Id": "42ff68ac-1bec-89bc-df9e-63efaddc5e8c",
        "IsConnected": true,
        "CacheMode": 1,
        "AutoRefreshCache": true,
        "ProductId": "03535-092-6002024-02739",
        "ConnectionSettings": {
          "ServerName": "172.17.2.11",
          "TieredManagementGroupConnectionSettings": null,
          "InactivityTimeout": 60,
          "SendReceiveTimeout": 30,
          "UserName": "csm",
          "Password": {
            "Length": 11
          },
          "Domain": "afranet",
          "CacheMode": 1,
          "CacheConfiguration": 0,
          "ThreeLetterWindowsLanguageName": "ENU"
        },
        "Version": {
          "Major": 7,
          "Minor": 5,
          "Build": 3079,
          "Revision": 0,
          "MajorRevision": 0,
          "MinorRevision": 0
        },
        "CurrentCountryCode": "ENU",
        "OriginalCountryCode": "ENU",
        "Registry": {},
        "Extensions": {},
        "LifetimeManagers": {
          "RegisteredTypes": [
            "External",
            "Singleton",
            "PerformanceTracing"
          ]
        },
        "Features": {}
      },
      "ManagementGroupId": "42ff68ac-1bec-89bc-df9e-63efaddc5e8c",
      "Identifier": {
        "Value": "1|ManagementPack.3860a76b540a44e2be6aa8c94b23e544|1.0.0.0|Template.a98f4f80e2a44454854fe314a3a98aab||"
      },
      "Name": "Template.a98f4f80e2a44454854fe314a3a98aab",
      "Id": "135696e7-8017-39b6-0588-4e1e255ad930",
      "DisplayName": "DC - Virtual Server Installation",
      "Description": null,
      "LanguageCode": "ENU",
      "Comment": null,
      "Status": 0,
      "LastModified": "2016-11-23T09:54:03.04",
      "TimeAdded": "2016-11-07T12:48:28.807",
      "InstanceName": null
    }
  },
  {
    "RequestOffering": "Access to Monitoring",
    "ServiceOffering": "DC - VPS",
    "CategoryGUID": "Enum.176787fee69b4feabae2ac0a02d1d7dc",
    "CategoryDisplayName": "Data Center",
    "TemplateName": "Template.39732e9d603942bc815e64e79d39d124",
    "TemplateObject": {
      "TypeID": {
        "Name": null,
        "Id": "9dcf1f69-e495-af57-a30e-741af1836bd2",
        "Identifier": {
          "Value": "1|PishroSystem.ServiceRequest.Form/7a1fbae719247839|1.0.0.0|Afranet.ServiceRequest.Form_TypeProjection||"
        }
      },
      "TypeConstraint": null,
      "PropertyCollection": [],
      "ObjectCollection": [
        {
          "Path": "$Context/Path[Relationship='CustomSystem_WorkItem_Activity_Library!System.WorkItemContainsActivity' TypeConstraint='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity.ManualActivity']$",
          "PropertyCollection": [
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/SequenceId$",
              "MixedValue": "0"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/ChildId$",
              "MixedValue": "5835"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/Skip$",
              "MixedValue": "False"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Title$",
              "MixedValue": "Access to monitoring"
            },
            {
              "Path": "$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Description$",
              "MixedValue": "باسلام; لطفا دسترسی مانیتورینگ را فعال کنید"
            }
          ],
          "ObjectCollection": []
        }
      ],
      "XmlTag": "ObjectTemplate",
      "ManagementGroup": {
        "ConnectorFramework": {},
        "EntityTypes": {},
        "EntityObjects": {},
        "Knowledge": {},
        "LanguagePacks": {},
        "ManagementPacks": {},
        "Monitoring": {},
        "Overrides": {},
        "Presentation": {},
        "Dashboard": {},
        "Reporting": {},
        "Resources": {},
        "Security": {},
        "Subscription": {},
        "TaskConfiguration": {},
        "TaskRuntime": {},
        "Templates": {},
        "TypeCache": {},
        "TypeDefinitions": {},
        "DataWarehouse": {},
        "Notifications": {},
        "ServiceLevelAgreements": {},
        "Deployment": {},
        "TaskConfigurationInternal": {},
        "LinkingFramework": {},
        "UserSettings": {},
        "Name": "AfraSCSM",
        "Id": "42ff68ac-1bec-89bc-df9e-63efaddc5e8c",
        "IsConnected": true,
        "CacheMode": 1,
        "AutoRefreshCache": true,
        "ProductId": "03535-092-6002024-02739",
        "ConnectionSettings": {
          "ServerName": "172.17.2.11",
          "TieredManagementGroupConnectionSettings": null,
          "InactivityTimeout": 60,
          "SendReceiveTimeout": 30,
          "UserName": "csm",
          "Password": {
            "Length": 11
          },
          "Domain": "afranet",
          "CacheMode": 1,
          "CacheConfiguration": 0,
          "ThreeLetterWindowsLanguageName": "ENU"
        },
        "Version": {
          "Major": 7,
          "Minor": 5,
          "Build": 3079,
          "Revision": 0,
          "MajorRevision": 0,
          "MinorRevision": 0
        },
        "CurrentCountryCode": "ENU",
        "OriginalCountryCode": "ENU",
        "Registry": {},
        "Extensions": {},
        "LifetimeManagers": {
          "RegisteredTypes": [
            "External",
            "Singleton",
            "PerformanceTracing"
          ]
        },
        "Features": {}
      },
      "ManagementGroupId": "42ff68ac-1bec-89bc-df9e-63efaddc5e8c",
      "Identifier": {
        "Value": "1|ManagementPack.3860a76b540a44e2be6aa8c94b23e544|1.0.0.0|Template.39732e9d603942bc815e64e79d39d124||"
      },
      "Name": "Template.39732e9d603942bc815e64e79d39d124",
      "Id": "e00a8acf-f263-104c-b6c8-cfb433f06ad1",
      "DisplayName": "Access to Monitoring",
      "Description": null,
      "LanguageCode": "ENU",
      "Comment": null,
      "Status": 0,
      "LastModified": "2016-11-23T12:11:57.723",
      "TimeAdded": "2016-11-23T08:59:29.94",
      "InstanceName": null
    }
  }
]"

我的课程是:

public class SOROCat
    {
        public String RequestOffering { get; set; }
        public String ServiceOffering { get; set; }
        public String CategoryGUID { get; set; }
        public String CategoryDisplayName { get; set; }
        public String TemplateName { get; set; }
        public ManagementPackObjectTemplate TemplateObject { get; set; }
    }

这是我的代码:

var response4 = await client.GetAsync("api/GetServiceRequestServiceRequestOffering");
response4.EnsureSuccessStatusCode();
string data = await response4.Content.ReadAsStringAsync();
List<SOROCat> genericEnum = JsonConvert.DeserializeObject<List<SOROCat>>(data);

错误讯息:  无法从System.String转换或转换为System.Collections.Generic.List`1 [SCSMModel.SOROCat]

3 个答案:

答案 0 :(得分:0)

下面使用JavaScriptSerializer类将字符串反序列化为给定的预期格式,是否可以尝试此解决方案

string data = await response4.Content.ReadAsStringAsync();
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
return jsSerializer.Deserialize<T>(data);

您必须指定预期类型。

答案 1 :(得分:0)

我必须发表评论ManagementPackObjectTemplate,我不知道ManagementPackObjectTemplate

这是我的班级:

public class SOROCat
    {
        public String RequestOffering { get; set; }
        public String ServiceOffering { get; set; }
        public String CategoryGUID { get; set; }
        public String CategoryDisplayName { get; set; }
        public String TemplateName { get; set; }
        //public ManagementPackObjectTemplate TemplateObject { get; set; }
    }

之后我有一些错误转换字符串\r\n所以我使用这个链接: link

var jsonStr = JsonConvert.DeserializeObject<string>(data);
                var genericEnum = JsonConvert.DeserializeObject<List<SOROCat>>(jsonStr, new JsonSerializerSettings
                {
                    TypeNameHandling = TypeNameHandling.Auto,
                    Formatting = Formatting.Indented
                });

答案 2 :(得分:-1)

使用DeserializeObject的通用版本

SOROCat soroObj= JsonConvert.DeserializeObject<SOROCat>(json);

foreach (string str in soroObj.type[0])
{
    // TODO with str
}

供参考,您可以使用this link