[英]Deserializing JSON string into C# list
how to Deserialize JSON to List<SOROCat>
in C#, please help me. 如何在C# List<SOROCat>
JSON反序列List<SOROCat>
,请帮助我。 Not able to get where I am going wrong. 无法找到我要去的地方。 Any help would be really appreciated. 任何帮助将非常感激。
The JSON is as follows. 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
}
}
]"
my class is : 我的课是:
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; }
}
this is my code : 这是我的代码:
var response4 = await client.GetAsync("api/GetServiceRequestServiceRequestOffering");
response4.EnsureSuccessStatusCode();
string data = await response4.Content.ReadAsStringAsync();
List<SOROCat> genericEnum = JsonConvert.DeserializeObject<List<SOROCat>>(data);
Error message: Could not cast or convert from System.String to System.Collections.Generic.List`1[SCSMModel.SOROCat] 错误消息:无法从System.String强制转换或转换为System.Collections.Generic.List`1 [SCSMModel.SOROCat]
The below uses JavaScriptSerializer class to deserialize the string to the expected format given, could you please try this solution 下面使用JavaScriptSerializer类将字符串反序列化为给定的预期格式,请您尝试此解决方案
string data = await response4.Content.ReadAsStringAsync();
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
return jsSerializer.Deserialize<T>(data);
In the place of you have to specify your expected type. 您必须指定所需的类型。
i have to comment ManagementPackObjectTemplate
and i have no idea for ManagementPackObjectTemplate
我必须评论ManagementPackObjectTemplate
,我不知道ManagementPackObjectTemplate
this is my class: 这是我的课:
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; }
}
after that I have some errors for converting the string \\r\\n
so I use this link: link 之后,我在转换字符串\\r\\n
遇到一些错误,因此我使用以下链接: link
var jsonStr = JsonConvert.DeserializeObject<string>(data);
var genericEnum = JsonConvert.DeserializeObject<List<SOROCat>>(jsonStr, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
Formatting = Formatting.Indented
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.