我遇到过一个反序列化标准但相当大的JSON的特定条件。
{
"fields": [
{
"webhook": true,
"associated_lookup": null,
"json_type": "jsonobject",
"crypt": null,
"field_label": "Account Owner",
"tooltip": null,
"created_source": "default",
"field_read_only": true,
"section_id": 1,
"read_only": false,
"businesscard_supported": true,
"currency": {},
"id": "3565223000000002421",
"custom_field": false,
"lookup": {},
"visible": true,
"length": 120,
"view_type": {
"view": true,
"edit": true,
"quick_create": false,
"create": true
},
"subform": null,
"api_name": "Owner",
"unique": {},
"data_type": "ownerlookup",
"formula": {},
"decimal_place": null,
"pick_list_values": [],
"multiselectlookup": {},
"auto_number": {}
},
{
"webhook": true,
"associated_lookup": null,
"json_type": "string",
"crypt": null,
"field_label": "Rating",
"tooltip": null,
"created_source": "default",
"field_read_only": false,
"section_id": 1,
"read_only": false,
"businesscard_supported": true,
"currency": {},
"id": "3565223000000002423",
"custom_field": false,
"lookup": {},
"visible": true,
"length": 120,
"view_type": {
"view": true,
"edit": true,
"quick_create": false,
"create": true
},
"subform": null,
"api_name": "Rating",
"unique": {},
"data_type": "picklist",
"formula": {},
"decimal_place": null,
"pick_list_values": [
{
"display_value": "-None-",
"actual_value": "-None-"
},
{
"display_value": "Acquired",
"actual_value": "Acquired"
},
{
"display_value": "Active",
"actual_value": "Active"
},
{
"display_value": "Market Failed",
"actual_value": "Market Failed"
},
{
"display_value": "Project Cancelled",
"actual_value": "Project Cancelled"
},
{
"display_value": "Shut Down",
"actual_value": "ShutDown"
}
],
"multiselectlookup": {},
"auto_number": {}
},{
"webhook": true,
"associated_lookup": null,
"json_type": "string",
"crypt": null,
"field_label": "Account Type",
"tooltip": null,
"created_source": "default",
"field_read_only": false,
"section_id": 1,
"read_only": false,
"businesscard_supported": true,
"currency": {},
"id": "3565223000000002441",
"custom_field": false,
"lookup": {},
"visible": true,
"length": 120,
"view_type": {
"view": true,
"edit": true,
"quick_create": false,
"create": true
},
"subform": null,
"api_name": "Account_Type",
"unique": {},
"data_type": "picklist",
"formula": {},
"decimal_place": null,
"pick_list_values": [
{
"display_value": "-None-",
"actual_value": "-None-"
},
{
"display_value": "Analyst",
"actual_value": "Analyst"
},
{
"display_value": "Competitor",
"actual_value": "Competitor"
},
{
"display_value": "Customer",
"actual_value": "Customer"
},
{
"display_value": "Distributor",
"actual_value": "Distributor"
},
{
"display_value": "Integrator",
"actual_value": "Integrator"
},
{
"display_value": "Investor",
"actual_value": "Investor"
},
{
"display_value": "Other",
"actual_value": "Other"
},
{
"display_value": "Partner",
"actual_value": "Partner"
},
{
"display_value": "Press",
"actual_value": "Press"
},
{
"display_value": "Prospect",
"actual_value": "Prospect"
},
{
"display_value": "Reseller",
"actual_value": "Reseller"
},
{
"display_value": "Supplier",
"actual_value": "Supplier"
},
{
"display_value": "Vendor",
"actual_value": "Vendor"
}
],
"multiselectlookup": {},
"auto_number": {}
}
]}
我已经附上了JSON的一部分。我的反序列化和处理的C#代码已经提供如下。我期待做的是为数据类型标记为拾取列表的元素制作一个内部标记“pick_list_values”的链接列表(“data_type”:“选择列表”)。
JObject ZohoCRMFieldsJO = JObject.Parse(ZohoCRMFieldsJson);
IList<ZohoCRMFields> newZohoCRMField = ZohoCRMFieldsJO["fields"].Select(p => new ZohoCRMFields
{
zohocrmmoduleid = zohocrmmoduleid.ToString(),
webhook = (bool)p["webhook"],
json_type = (string)p["json_type"],
field_label = (string)p["field_label"],
created_source = (string)p["created_source"],
field_read_only = (bool)p["field_read_only"],
section_id = (string)p["section_id"],
read_only = (bool)p["read_only"],
id = (string)p["id"],
custom_field = (string)p["custom_field"],
length = (int)p["length"],
visible = (bool)p["visible"],
api_name = (string)p["api_name"],
data_type = (string)p["data_type"]
}).ToList();
foreach (var ZohoCRMField in newZohoCRMField)
{
if (ZohoCRMField.data_type == "picklist" )
{
var ZohoCRMPickListsProvider = new ZohoCRMPickListsProvider();
IList<ZohoCRMPickLists> newZohoCRMPickList = ZohoCRMFieldsJO["fields"].Select(p => new ZohoCRMPickLists
{
zohocrmpicklistmoduleid = zohocrmmoduleid,
zohocrmfieldid = (string)p["id"],
display_value = (string)p[newZohoCRMField.IndexOf(ZohoCRMField)]["pick_list_values"]["display_value"],
actual_value = (string)p[newZohoCRMField.IndexOf(ZohoCRMField)]["pick_list_values"]["actual_value"]
}).ToList();
}
}
如何完成反序列化。需要获取与数据类型为pickuplist的节对应的pickuplist。任何合适的替代方法都可以,但输出必须在LinkedList〈〉中。
1条答案
按热度按时间tp5buhyn1#
不使用JObject.Parse()解析json字符串,您可以创建一个与您的json结构匹配的模型,反序列化字符串,然后使用linq获取您需要的数据。
C#模型:
反序列化json并获取值:
型