Get information from json File by KeyName

Carlo Goretti 121 Reputation points
2021-05-25T17:06:44.727+00:00

Hey,

Im wondering if there is some better way to get info from a json file?
Today this is my way of doing this:

Here is the Json file:

{
    "Language": "Svenska",
    "Properties": [
      //Labels
      {
        "KeyName": "UserregistrationHeaderLbl",
        "Value": "User Registration"
      },
      {
        "KeyName": "FullNameLbl",
        "Value": "Full name"
      }]
}


            Assembly assembly = Assembly.GetExecutingAssembly();
            Stream jsonFile = assembly.GetManifestResourceStream("testjson.json");
            StreamReader r = new StreamReader(jsonFile, Encoding.UTF7);
            string json = r.ReadToEnd();

            languages = JsonConvert.DeserializeObject<List<Languages>>(json);
           // Defaults = JsonConvert.DeserializeObject<List<Languages>>(json);
            //var test = JsonConvert.DeserializeObject<Definition.Configuration>(configurationFile);

            for (int i = 0; i < languages.Count(); i++)
            {
                if (languages[i].Language =="Svenska")
                {
                    for (int a = 0; a < languages[i].Properties.Count(); a++)
                    {
                        TrainingHeaderLbl.Text = languages[i].Properties[0].Value;
                        TrainingStyleLbl.Text = languages[i].Properties[1].Value;
                        TrainingLevelLbl.Text = languages[i].Properties[2].Value;

                        TrainingStyleRBCondition.Content = languages[i].Properties[3].Value;
                        TrainingStyleRBStyrkeTräning.Content = languages[i].Properties[4].Value;
                        TrainingStyleRBMix.Content = languages[i].Properties[5].Value;

                        TrainingLevelRBBasic.Content = languages[i].Properties[6].Value;
                        TrainingLevelRBMedium.Content = languages[i].Properties[7].Value;
                        TrainingLevelRBAdvance.Content = languages[i].Properties[8].Value;

                        ContinueButton.Text = languages[i].Properties[9].Value;
                    }
                }
            }

Can i somehow do it like this?

TrainingStyleLbl.Text = languages[i].Properties["UserregistrationHeaderLbl"].Value;

Thankful for some help or some advices.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,261 questions
0 comments No comments
{count} votes

Accepted answer
  1. Kyle Wang 5,526 Reputation points
    2021-05-26T06:55:46.787+00:00

    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.


0 additional answers

Sort by: Most helpful