0

I need to serialize a response of an object with a dictionary dynamically. Left the json examples.

I am trying to serialize this response object (request_validator) in a C# class, but this is not working. Can someone help me please, any ideas?

{
    "person": {
        "testing": "CC",
        "simple": "1234545",
        "errorNames": {
            "id": "655789",
            "error": "simple"
        },
        "errorColor": {
            "id": "2",
            "error": "error color"
        }
    }
}

{
    "request_validator": [
        {
            "person.errorNames": [
                "error names"
            ],
            "person.errorColor": [
                "error color"
            ]
        }
    ]
}

Code:

public class DeserializeResponse
{
    public Dictionary<string, List<string>> request_validator { get; set; }
}

var error = JsonConvert.DeserializeObject<List<DeserializeResponse>>(content);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Manu
  • 3
  • 3
  • Does this answer your question? [How to auto-generate a C# class file from a JSON string](https://stackoverflow.com/questions/21611674/how-to-auto-generate-a-c-sharp-class-file-from-a-json-string) – Self Apr 29 '21 at 14:46
  • 1
    `public List>> RequestValidator { get; set; }`, The first chart after the property name is an `[` it's used for List/array. So RequestValidator is not a dict. it's a list that containst that dict. – Self Apr 29 '21 at 14:47
  • 1
    `"request_validator"` is array of objects, not an object. Dictionaries usually are mapped from objects. – Guru Stron Apr 29 '21 at 14:48

2 Answers2

0
public class DeserializeResponse
{
    [JsonPropertyName("request_validator")]
    public RequestValidator[] RequestValidator { get; set; }
}

public class RequestValidator
{
    [JsonPropertyName("person.errorNames")]
    public string[] PersonErrorNames { get; set; }

    [JsonPropertyName("person.errorColor")]
    public string[] PersonErrorColor { get; set; }
}

...

var error = JsonSerializer.Deserialize<DeserializeResponse>(content);
Victor
  • 46
  • 3
  • hi victor, thanks for the answer, is a good practice ? because the response errors are dynamic { [JsonPropertyName("person.errorNames")] public string[] PersonErrorNames { get; set; } [JsonPropertyName("person.errorColor")] public string[] PersonErrorColor { get; set; } [JsonPropertyName("person.errorX")] public string[] PersonErrorX { get; set; } [JsonPropertyName("person.errorY")] public string[] PersonErrorY { get; set; } [JsonPropertyName("person.errorZ")] public string[] PersonErrorZ { get; set; } } – Manu Apr 29 '21 at 15:31
  • hi victor, thanks for the answer, How to create those properties dynamically for the RequestValidator class, so as not to repeat code, is it possible? – Manu Apr 29 '21 at 15:41
  • json serializer will not allow us to read the values if we don't specify json property – Victor Apr 29 '21 at 17:04
  • if the properties in json are dynamic then this approach will not work, but if there are a lot of properties and they do not change, then you can use the service (https://app.quicktype.io) to generate classes – Victor Apr 29 '21 at 17:13
0

You can use Newtonsoft.Json library to get all properties from string array into dictionary

in this case you just need to point to the searched level

using Newtonsoft.Json.Linq;
...
            JObject validator = JObject.Parse(content);
            IJEnumerable<JToken> validatorTokens = validator.SelectTokens("request_validator")?.Values();
            Dictionary<string, List<string>> errors = new Dictionary<string, List<string>>();
            if (validatorTokens != null)
            {
                foreach (JProperty prop in validatorTokens.Values())
                {
                    if (!errors.ContainsKey(prop.Name))
                    {
                        errors.Add(prop.Name, new List<string>());
                    }
                    errors[prop.Name].Add(prop.Value?.ToString());
                }
            }
Victor
  • 46
  • 3