0

I am trying to deserialize a complex json that i am getting via an api call. I have 2 questions.

  1. This is the json that i tried to convert to class on site https://json2csharp.com/json-to-csharp
{
    "info": {
        "total_count": 2,
        "current_page_total_count": 2,
        "max_each_page": 500,
        "current_page_number": 1,
        "total_pages": 1
    },
    "entity_data": {
        "entity_name": "\u05de\u05d0\u05d9\u05e8",
        "entity_data_name": "e_130",
        "permissions": {
            "type": "full",
            "code": "1"
        },
        "view": "data table"
    },
    "data": [
        {
            "instance_data": {
                "_id": "60a3a82a956ecd3228739ced",
                "insertTimestamp": "2021-05-18T14:42:34+0300",
                "field_groups": [
                    {
                        "field_group_data": {
                            "group_name": "\u05e1\u05e4\u05e8 \u05d8\u05dc\u05e4\u05d5\u05e0\u05d9\u05dd",
                            "group_data_name": "g_353",
                            "group_id": "353",
                            "repeatable_group": "0",
                            "permissions": {
                                "type": "full",
                                "code": "1"
                            }
                        },
                        "fields_data": [
                            [
                                {
                                    "field_name": "\u05e9\u05dd \u05e4\u05e8\u05d8\u05d9",
                                    "field_type_name": "input-text",
                                    "field_data_name": "fld_2372",
                                    "placeholder": null,
                                    "default_value": "",
                                    "act_as_assign": "",
                                    "custom_validation": {
                                        "unable_to_edit": "0",
                                        "data_type": "0"
                                    },
                                    "value": "\u05de\u05d0\u05d9\u05e8",
                                    "group_index": 0
                                },
                                {
                                    "field_name": "\u05e9\u05dd \u05de\u05e9\u05e4\u05d7\u05d4",
                                    "field_type_name": "input-text",
                                    "field_data_name": "fld_2373",
                                    "placeholder": null,
                                    "default_value": "",
                                    "act_as_assign": "",
                                    "custom_validation": {
                                        "unable_to_edit": "0",
                                        "data_type": "0"
                                    },
                                    "value": "\u05e8\u05d5\u05d8\u05e4\u05dc\u05d9\u05d9\u05e9",
                                    "group_index": 0
                                },
                                {
                                    "field_name": "\u05d9\u05d9\u05e9\u05d5\u05d1 \u05de\u05d2\u05d5\u05e8\u05d9\u05dd",
                                    "field_type_name": "select-list",
                                    "field_data_name": "fld_2374",
                                    "placeholder": null,
                                    "default_value": "",
                                    "possible_values": "[\"\\u05db\\u05e4\\u05e8 \\u05e1\\u05d1\\u05d0\",\"\\u05ea\\u05dc \\u05d0\\u05d1\\u05d9\\u05d1\",\"\\u05e2\\u05d6\\u05d4\",\"\\u05d9\\u05e8\\u05d5\\u05e9\\u05dc\\u05d9\\u05dd\"]",
                                    "act_as_assign": "",
                                    "custom_validation": {
                                        "unable_to_edit": "0",
                                        "alphabetical_order": "0"
                                    },
                                    "value": "\u05db\u05e4\u05e8 \u05e1\u05d1\u05d0",
                                    "group_index": 0
                                },
                                {
                                    "field_name": "\u05d8\u05dc\u05e4\u05d5\u05df",
                                    "field_type_name": "input-text",
                                    "field_data_name": "fld_2375",
                                    "placeholder": null,
                                    "default_value": "",
                                    "act_as_assign": "",
                                    "custom_validation": {
                                        "unable_to_edit": "0",
                                        "data_type": "0"
                                    },
                                    "value": "055098393",
                                    "group_index": 0
                                },
                                {
                                    "field_name": "\u05de\u05d9\u05d9\u05dc",
                                    "field_type_name": "input-email",
                                    "field_data_name": "fld_2376",
                                    "placeholder": null,
                                    "default_value": "",
                                    "act_as_assign": "",
                                    "custom_validation": {
                                        "unable_to_edit": "0"
                                    },
                                    "value": "meir@arma.co.il",
                                    "group_index": 0
                                }
                            ]
                        ]
                    }
                ]
            },
            "single_permission": null
        },
        {
            "instance_data": {
                "_id": "60a3a7e6788c2124f12b5370",
                "insertTimestamp": "2021-05-18T14:41:26+0300",
                "field_groups": [
                    {
                        "field_group_data": {
                            "group_name": "\u05e1\u05e4\u05e8 \u05d8\u05dc\u05e4\u05d5\u05e0\u05d9\u05dd",
                            "group_data_name": "g_353",
                            "group_id": "353",
                            "repeatable_group": "0",
                            "permissions": {
                                "type": "full",
                                "code": "1"
                            }
                        },
                        "fields_data": [
                            [
                                {
                                    "field_name": "\u05e9\u05dd \u05e4\u05e8\u05d8\u05d9",
                                    "field_type_name": "input-text",
                                    "field_data_name": "fld_2372",
                                    "placeholder": null,
                                    "default_value": "",
                                    "act_as_assign": "",
                                    "custom_validation": {
                                        "unable_to_edit": "0",
                                        "data_type": "0"
                                    },
                                    "value": "\u05d0\u05e8\u05d9\u05d4",
                                    "group_index": 0
                                },
                                {
                                    "field_name": "\u05e9\u05dd \u05de\u05e9\u05e4\u05d7\u05d4",
                                    "field_type_name": "input-text",
                                    "field_data_name": "fld_2373",
                                    "placeholder": null,
                                    "default_value": "",
                                    "act_as_assign": "",
                                    "custom_validation": {
                                        "unable_to_edit": "0",
                                        "data_type": "0"
                                    },
                                    "value": "\u05de\u05e6\u05dc\u05d9\u05d7",
                                    "group_index": 0
                                },
                                {
                                    "field_name": "\u05d9\u05d9\u05e9\u05d5\u05d1 \u05de\u05d2\u05d5\u05e8\u05d9\u05dd",
                                    "field_type_name": "select-list",
                                    "field_data_name": "fld_2374",
                                    "placeholder": null,
                                    "default_value": "",
                                    "possible_values": "[\"\\u05db\\u05e4\\u05e8 \\u05e1\\u05d1\\u05d0\",\"\\u05ea\\u05dc \\u05d0\\u05d1\\u05d9\\u05d1\",\"\\u05e2\\u05d6\\u05d4\",\"\\u05d9\\u05e8\\u05d5\\u05e9\\u05dc\\u05d9\\u05dd\"]",
                                    "act_as_assign": "",
                                    "custom_validation": {
                                        "unable_to_edit": "0",
                                        "alphabetical_order": "0"
                                    },
                                    "value": "\u05e2\u05d6\u05d4",
                                    "group_index": 0
                                },
                                {
                                    "field_name": "\u05d8\u05dc\u05e4\u05d5\u05df",
                                    "field_type_name": "input-text",
                                    "field_data_name": "fld_2375",
                                    "placeholder": null,
                                    "default_value": "",
                                    "act_as_assign": "",
                                    "custom_validation": {
                                        "unable_to_edit": "0",
                                        "data_type": "0"
                                    },
                                    "value": "055098393",
                                    "group_index": 0
                                },
                                {
                                    "field_name": "\u05de\u05d9\u05d9\u05dc",
                                    "field_type_name": "input-email",
                                    "field_data_name": "fld_2376",
                                    "placeholder": null,
                                    "default_value": "",
                                    "act_as_assign": "",
                                    "custom_validation": {
                                        "unable_to_edit": "0"
                                    },
                                    "value": "arie@arma.co.il",
                                    "group_index": 0
                                }
                            ]
                        ]
                    }
                ]
            },
            "single_permission": null
        }
    ]
}

when I put the class into VS i get an error on the following

public class FieldGroup
{
    public FieldGroupData field_group_data { get; set; }
    public List<List<>> fields_data { get; set; }
}

on List<List<>

Unexpected use of an unbound generic name

  1. I am at present saving the json to a file how do i deserialize it with the classes generated?
dbc
  • 104,963
  • 20
  • 228
  • 340
  • 1
    @stuart ..Thanks I tried to edit a few times to get the code into the the code format didnt work for me !? need to learn how the editor works :) – Meir Rotfleisch May 23 '21 at 09:07
  • no worries, let me know if my answer solves your issue – Stuart Wakefield May 23 '21 at 18:39
  • If https://json2csharp.com/json-to-csharp creates syntactically invalid c# you could try some other tool from [How to auto-generate a C# class file from a JSON string](https://stackoverflow.com/q/21611674/3744182). – dbc May 23 '21 at 19:07
  • 1
    https://app.quicktype.io/#l=cs&r=json2csharp seems to work, see https://dotnetfiddle.net/zqjduq – dbc May 23 '21 at 19:12

2 Answers2

0

In your particular example, the converter seems to have fallen over the list of lists:

...
"fields_data": [
    [
        {
            "field_name": "\u05e9\u05dd \u05e4\u05e8\u05d8\u05d9",
            "field_type_name": "input-text",
            "field_data_name": "fld_2372",
            "placeholder": null,
            "default_value": "",
            "act_as_assign": "",
            "custom_validation": {
                "unable_to_edit": "0",
                "data_type": "0"
            },
            "value": "\u05de\u05d0\u05d9\u05e8",
            "group_index": 0
        }
        ...

To work around this problem, you can then perform a second conversion by grabbing one of the items from the list, i.e.:

{
    "field_name": "\u05e9\u05dd \u05e4\u05e8\u05d8\u05d9",
    "field_type_name": "input-text",
    "field_data_name": "fld_2372",
    "placeholder": null,
    "default_value": "",
    "act_as_assign": "",
    "custom_validation": {
        "unable_to_edit": "0",
        "data_type": "0"
    },
    "value": "\u05de\u05d0\u05d9\u05e8",
    "group_index": 0
}

And repeating the conversion process. Simply rename the Root class to something useful, such as Field and add this in the List<List<>> definition, i.e. List<List<Field>>. The resulting classes may look as follows:

// These are the new classes from the second conversion
public class CustomValidation
{
    public string unable_to_edit { get; set; }
    public string data_type { get; set; }
}

// Renamed this from Root to Field
public class Field
{
    public string field_name { get; set; }
    public string field_type_name { get; set; }
    public string field_data_name { get; set; }
    public object placeholder { get; set; }
    public string default_value { get; set; }
    public string act_as_assign { get; set; }
    public CustomValidation custom_validation { get; set; }
    public string value { get; set; }
    public int group_index { get; set; }
}

// These are the original classes from the first conversion attempt
public class Info
{
    public int total_count { get; set; }
    public int current_page_total_count { get; set; }
    public int max_each_page { get; set; }
    public int current_page_number { get; set; }
    public int total_pages { get; set; }
}

public class Permissions
{
    public string type { get; set; }
    public string code { get; set; }
}

public class EntityData
{
    public string entity_name { get; set; }
    public string entity_data_name { get; set; }
    public Permissions permissions { get; set; }
    public string view { get; set; }
}

public class FieldGroupData
{
    public string group_name { get; set; }
    public string group_data_name { get; set; }
    public string group_id { get; set; }
    public string repeatable_group { get; set; }
    public Permissions permissions { get; set; }
}

public class FieldGroup
{
    public FieldGroupData field_group_data { get; set; }

    // Updated this field type to include the Field class
    public List<List<Field>> fields_data { get; set; }
}

public class InstanceData
{
    public string _id { get; set; }
    public DateTime insertTimestamp { get; set; }
    public List<FieldGroup> field_groups { get; set; }
}

public class Datum
{
    public InstanceData instance_data { get; set; }
    public object single_permission { get; set; }
}

public class Root
{
    public Info info { get; set; }
    public EntityData entity_data { get; set; }
    public List<Datum> data { get; set; }
}

This is merely a starting point, so make sure to rename the classes to make sense to your application (for example Root might not be that descriptive) and remove any fields that you are not using (as the deserializer will then happily ignore them.)

To deserialize you can use the JsonSerializer in System.Text.Json if you are on later versions of .NET Core: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-overview...

Root root = JsonSerializer.Deserialize<Root>(json); 

Note that you may have issues with the date time format being used. If you do not use this field you can simply remove it from your classes it otherwise you will need to specify the date time converter:

JsonSerializerOptions options = new JsonSerializerOptions();
options.Converters.Add(new DateTimeConverterUsingDateTimeParse());
Root root = JsonSerializer.Deserialize<Root>(json, options); 
Stuart Wakefield
  • 6,294
  • 24
  • 35
0

I was able to convert the json to class with no problems.

EDIT

Oh! I understand now. The json-to-csharp parser didn't convert a part of the json to c-sharp. has pointed out above, just get the part of the json that was not parse and try converting that alone so the below json becomes...

    {
        "field_name": "\u05e9\u05dd \u05e4\u05e8\u05d8\u05d9",
        "field_type_name": "input-text",
        "field_data_name": "fld_2372",
        "placeholder": null,
        "default_value": "",
        "act_as_assign": "",
        "custom_validation": {
            "unable_to_edit": "0",
            "data_type": "0"
        },
        "value": "\u05de\u05d0\u05d9\u05e8",
        "group_index": 0
    }

//The charp object for the json above

public class CustomValidation
{
    public string unable_to_edit { get; set; }
    public string data_type { get; set; }
}

public class Field
{
    public string field_name { get; set; }
    public string field_type_name { get; set; }
    public string field_data_name { get; set; }
    public object placeholder { get; set; }
    public string default_value { get; set; }
    public string act_as_assign { get; set; }
    public CustomValidation custom_validation { get; set; }
    public string value { get; set; }
    public int group_index { get; set; }
}

//then correct "fields_data" type (List<List<>>) to "List<List<Field>>" 

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
public class Info
{
    public int total_count { get; set; }
    public int current_page_total_count { get; set; }
    public int max_each_page { get; set; }
    public int current_page_number { get; set; }
    public int total_pages { get; set; }
}

public class Permissions
{
    public string type { get; set; }
    public string code { get; set; }
}

public class EntityData
{
    public string entity_name { get; set; }
    public string entity_data_name { get; set; }
    public Permissions permissions { get; set; }
    public string view { get; set; }
}

public class FieldGroupData
{
    public string group_name { get; set; }
    public string group_data_name { get; set; }
    public string group_id { get; set; }
    public string repeatable_group { get; set; }
    public Permissions permissions { get; set; }
}

public class FieldGroup
{
    public FieldGroupData field_group_data { get; set; }
    //correct "fields_data" type (List<List<>>) to "List<List<Field>>"
    //public List<List<>> fields_data { get; set; }
    public List<List<Field>> fields_data { get; set; }//List<List<Field>>
}

public class InstanceData
{
    public string _id { get; set; }
    public DateTime insertTimestamp { get; set; }
    public List<FieldGroup> field_groups { get; set; }
}

public class Datum
{
    public InstanceData instance_data { get; set; }
    public object single_permission { get; set; }
}

public class Root
{
    public Info info { get; set; }
    public EntityData entity_data { get; set; }
    public List<Datum> data { get; set; }
}