Hi CarloGoretti-1185,
Welcome to our Microsoft Q&A platform!
If you want to get the "Value" based on "KeyName", you can use LINQ query instead of "Properties["UserregistrationHeaderLbl"]" you mentioned.
string json = File.ReadAllText(@"testjson.json");
Languages test = JsonConvert.DeserializeObject<Languages>(json);
string language = test.Language;
KeyValueCalss kv = test.Properties.Where(x => x.KeyName == "UserregistrationHeaderLbl").FirstOrDefault();
string value = kv.Value;
If you want to get Value like a dictionary, you can declare a new dictionary property to store the data.
class Languages
{
[JsonProperty("Language")]
public string Language { get; set; }
[JsonProperty("Properties")]
public List<KeyValueCalss> Properties { get; set; }
Dictionary<string, string> keyValuePairs;
public Dictionary<string, string> KeyValuePairs { get => keyValuePairs; }
public void SetDictionary()
{
keyValuePairs = new Dictionary<string, string>();
foreach (KeyValueCalss kv in Properties)
{
keyValuePairs.Add(kv.KeyName, kv.Value);
}
}
}
Then call "SetDictionary" to initial the dictionary.
test.SetDictionary();
string value = test.KeyValuePairs["UserregistrationHeaderLbl"];
Here is another workaround which you need to modify your json as follows.
{
"Language": "Svenska",
"Properties": {
"UserregistrationHeaderLbl": "User Registration",
"FullNameLbl": "Full name"
}
}
Language class:
class Languages
{
[JsonProperty("Language")]
public string Language { get; set; }
[JsonProperty("Properties")]
public Dictionary<string, string> Properties { get; set; }
}
Languages test = JsonConvert.DeserializeObject<Languages>(json);
string value = test.Properties["UserregistrationHeaderLbl"];
Regards,
Kyle
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.