Десериализация JSON с использованием JObject и LINQ - PullRequest
0 голосов
/ 31 октября 2018

Я столкнулся с определенным условием десериализации стандартного, но довольно большого 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»: «picklist»).

    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();

    }
}

Как я могу завершить десериализацию. список получения должен быть получен в соответствии с разделом, тип данных которого является списком выбора. Любые подходящие альтернативы также будут работать, но выходные данные должны быть в LinkedList <>.

1 Ответ

0 голосов
/ 31 октября 2018

Вместо анализа строки json с помощью JObject.Parse () вы можете создать модель, соответствующую структуре json, десериализовать строку и затем использовать linq, чтобы получить необходимые данные.

C # модели:

public class FieldList
{
    public List<Field> fields { get; set; }
}

public class Field
{
    public bool webhook { get; set; }
    public object associated_lookup { get; set; }
    public string json_type { get; set; }
    public object crypt { get; set; }
    public string field_label { get; set; }
    public object tooltip { get; set; }
    public string created_source { get; set; }
    public bool field_read_only { get; set; }
    public int section_id { get; set; }
    public bool read_only { get; set; }
    public bool businesscard_supported { get; set; }
    public Currency currency { get; set; }
    public string id { get; set; }
    public bool custom_field { get; set; }
    public Lookup lookup { get; set; }
    public bool visible { get; set; }
    public int length { get; set; }
    public ViewType view_type { get; set; }
    public object subform { get; set; }
    public string api_name { get; set; }
    public Unique unique { get; set; }
    public string data_type { get; set; }
    public Formula formula { get; set; }
    public object decimal_place { get; set; }
    public List<PickListValues> pick_list_values { get; set; }
    public MultiSelectLookup multiselectlookup { get; set; }
    public Auto_Number auto_number { get; set; }
}

public class Currency
{
}

public class Lookup
{
}

public class ViewType
{
    public bool view { get; set; }
    public bool edit { get; set; }
    public bool quick_create { get; set; }
    public bool create { get; set; }
}

public class Unique
{
}

public class Formula
{
}

public class MultiSelectLookup
{
}

public class Auto_Number
{
}

public class PickListValues
{
    public string display_value { get; set; }
    public string actual_value { get; set; }
}

Десериализация JSON и получение значений:

string json = "....";
var values = new List<PickListValues>();
var fieldList = JsonConvert.DeserializeObject<FieldList>(json) ?? new FieldList();

if (fieldList.fields != null)
{
    values = fieldList.fields.Where(x => x.data_type.Equals("picklist"))
                             .SelectMany(x => x.pick_list_values)
                             .ToList();
}

var linkedList = new LinkedList<PickListValues>(values);
...