question

CarloGoretti-1185 avatar image
0 Votes"
CarloGoretti-1185 asked CarloGoretti-1185 commented

Get information from json File by KeyName

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.


dotnet-xamarinformsdotnet-xamarinessentials
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

KyleWang-MSFT avatar image
0 Votes"
KyleWang-MSFT answered CarloGoretti-1185 commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the help! I did as the last one and that works great!

0 Votes 0 ·