Как добавить свойства во время выполнения к JSON (C#) - PullRequest
1 голос
/ 29 мая 2020

Примечание: я работаю с System.Text. Json package Ниже JSON я получаю из базы данных. Мне нужно go через каждый из ключей в JSON и проверить, есть ли точка (.) в имени ключа; если это так, мне нужно добавить свойство required со значением true в JSON, чтобы обеспечить проверку времени выполнения:

validate:{"required", true}

Вот мой JSON:

{
    "display": "wizard",
    "settings": {},
    "components": [{
        "title": "Event Information",
        "label": "Event Information",
        "type": "panel",
        "key": "EventInformation",
        "components": [{
            "label": "Row1Columns",
            "columns": [{
                "components": [{
                    "label": "Event Date",
                    "format": "dd/MM/yyyy hh:mm a",
                    "tableView": false,
                    "datePicker": {
                        "disableWeekends": false,
                        "disableWeekdays": false
                    },
                    "validate": {
                        "unique": true
                    },
                    "key": "Event.EventDate",
                    "type": "datetime",
                    "input": true,
                    "suffix": "<i ref=\"icon\" class=\"fa fa-calendar\" style=\"\"></i>",
                    "widget": {
                        "type": "calendar",
                        "displayInTimezone": "viewer",
                        "language": "en",
                        "useLocaleSettings": false,
                        "allowInput": true,
                        "mode": "single",
                        "enableTime": true,
                        "noCalendar": false,
                        "format": "dd/MM/yyyy hh:mm a",
                        "hourIncrement": 1,
                        "minuteIncrement": 1,
                        "time_24hr": false,
                        "minDate": null,
                        "disableWeekends": false,
                        "disableWeekdays": false,
                        "maxDate": null
                    }
                }],
                "width": 6,
                "offset": 0,
                "push": 0,
                "pull": 0
            }, {
                "components": [{
                    "label": "Duration (minutes)",
                    "mask": false,
                    "spellcheck": true,
                    "tableView": false,
                    "delimiter": false,
                    "requireDecimal": false,
                    "inputFormat": "plain",
                    "key": "Event.Duration",
                    "type": "number",
                    "input": true
                }],
                "width": 6,
                "offset": 0,
                "push": 0,
                "pull": 0
            }],
            "tableView": false,
            "key": "row1Columns",
            "type": "columns",
            "input": false
        }, {
            "label": "Row2Columns",
            "columns": [{
                "components": [{
                    "label": "Event Category",
                    "widget": "choicesjs",
                    "tableView": true,
                    "dataSrc": "custom",
                    "data": {
                        "custom": "values = getEventCategoryValues()"
                    },
                    "valueProperty": "AgencyEventCategoryId",
                    "template": "<span>{{ item.text }}</span>",
                    "selectThreshold": 0.3,
                    "validate": {
                        "required": true
                    },
                    "key": "Event.AgencyEventCategoryId",
                    "type": "select",
                    "indexeddb": {
                        "filter": {}
                    },
                    "input": true
                }],
                "width": 6,
                "offset": 0,
                "push": 0,
                "pull": 0
            }, {
                "components": [{
                    "label": "Attendance",
                    "widget": "choicesjs",
                    "tableView": true,
                    "multiple": false,
                    "dataSrc": "custom",
                    "data": {
                        "custom": "values = getAttendanceValues()"
                    },
                    "valueProperty": "AgencyEventAttendanceId",
                    "template": "<span>{{ item.text }}</span>",
                    "selectThreshold": 0.3,
                    "validate": {
                        "required": true,
                    },
                    "key": "Event.AgencyEventAttendanceId",
                    "type": "select",
                    "indexeddb": {
                        "filter": {}
                    },
                    "input": true
                }],
                "width": 6,
                "offset": 0,
                "push": 0,
                "pull": 0
            }],
            "tableView": false,
            "key": "row2Columns",
            "type": "columns",
            "input": false
        }, {
            "label": "Event Options",
            "widget": "choicesjs",
            "tableView": true,
            "multiple": true,
            "dataSrc": "custom",
            "data": {
                "custom": "values = getEventManagerValues(data.Event.AgencyEventCategoryId)"
            },
            "template": "<span>{{ item.text }}</span>",
            "refreshOn": "Event.AgencyEventCategoryId",
            "clearOnRefresh": true,
            "selectThreshold": 0.3,
            "calculateServer": false,
            "validate": {
                "required": true,
                "multiple": true
            },
            "key": "Event.EventDetail",
            "type": "select",
            "indexeddb": {
                "filter": {}
            },
            "input": true
        }, {
            "label": "Casenote",
            "wysiwyg": true,
            "autoExpand": true,
            "spellcheck": true,
            "tableView": true,
            "calculateServer": false,
            "key": "Event.EventCasenote[0].Casenote",
            "type": "textarea",
            "input": true
        }],
        "input": false,
        "tableView": false,
        "breadcrumbClickable": true,
        "buttonSettings": {
            "previous": true,
            "cancel": true,
            "next": true
        },
        "collapsible": false
    }]
} 

Ответы [ 3 ]

1 голос
/ 29 мая 2020

Один из вариантов - разобрать json в JObject и добавить к нему свойство через Json Newtonsoft. NET:

var obj = JObject.Parse("{'key':'value'}");
obj.Add("required", true);
Console.WriteLine(obj); // { "key": "value", "required": true }

Чтобы добавить новый объект, вы можете использовать эту перегрузку Add:

obj.Add("validate", JObject.FromObject(new { required = true }));

Таким образом, все решение будет выглядеть примерно так:

var obj = JObect.Parse(your_json);

foreach(var token in obj.DescendantsAndSelf().ToList()) // ToList is important!!!
{
    if(token is JObject xObj)
    {
        // check your conditions for adding property
        // check if object does not have "validate" property
        if(satisfies_all_conditions)
        {
            xObj.Add("validate", JObject.FromObject(new { required = true }));
        }
    }
}
0 голосов
/ 04 июня 2020

У меня была аналогичная проблема , и я нашел решение, см. Ниже код и комментарии. Я использую Newtonsoft, но стоит проверить, можете ли вы использовать библиотеку Newtonsoft и не нашли решения для System.Text.Json.

Все элементы управления в вашем JSON являются частью объекта-компонента / массив, чтобы мы могли использовать JSONPath, подробнее см. ссылку подробности .

public void IterateJson(JObject obj, string mandatoryFieldKey)
        {
JToken jTokenFoundForMandatoryField = obj.SelectToken
("$..components[?(@.key == '" + mandatoryFieldKey + "')]");   
            //Now we convert this oken into an object so that we can add properties/objects in it
            if (jTokenFoundForMandatoryField is JObject jObjectForMandatoryField)
            {
                //We check if validate already exists for this field, if it does not
 //exists then we add validate and required property inside the if condition
                if (jObjectForMandatoryField["validate"] == null)
                    jObjectForMandatoryField.Add("validate", 
JObject.FromObject(new { required = true })); //add validate and required property
                else
                {
                    //If validate does not exists then code comes here and 
//we convert the validate into a JObject using is JObject statement
                    if (jObjectForMandatoryField["validate"] is JObject validateObject)
                    {
                        //We need to check if required property already exists, 
//if it does not exists then we add it inside the if condition.
                        if (validateObject["required"] == null)
                        {
                            validateObject.Add("required", true); //add required property
                        }
                    }
                }                
            }  
}   
0 голосов
/ 29 мая 2020

Это была интересная задача для меня, вот что я написал

class Program
{
    static void Main(string[] args)
    {
        string jsonFilePath = @"test.json"; //path to your json
        string json = File.ReadAllText(jsonFilePath);
        var data = JObject.Parse(json);
        CheckJson(data);
        Console.ReadLine();
    }

    static void CheckJson(JToken value)
    {
        if (value.Values().Count() != 0) //if more than 0 - so value is object or array and we have to call this method for each property
        {
            foreach (var item in value.Values().ToList())
            {
                CheckJson(item);
            }
        }
        else if (true) //else - we have exactly value of key, which we can check, for example if . exists or additional checks
        {
            if (value.Parent.Parent is JObject jObject && jObject["validate"] == null) //check if above "required" property exists 
            {
                jObject.Add("validate", JObject.FromObject(new { required = true })); //add required property
            }
        }

    }
}

Этот код добавит в каждое свойство объекта "required", если оно не существует. Все, что вам нужно - просто добавьте метод проверки и вызовите его в блоке if . И лучше добавить дополнительную проверку, которая не будет продолжать проверку всех свойств, если существует «обязательное» свойство

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...