.NET C # convert ResourceSet to JSON

I would like to create a JSON object from a resource file (.resx). I converted it to a ResouceSet as such:

 ResourceSet resourceSet = MyResourceClass.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true); 

Now I have a set of objects in the form {Key:<key>, Value:<value>} , but instead I would like to convert it to JSON in the form or hash map {Key:Value, ...} .

+7
json c #
source share
2 answers

Since the ResourceSet is an old collection class (HashTable) and uses DictionaryEntry , you need to convert the resourceSet to Dictionary<string, string> and use Json.Net to serialize it:

 resourceSet.Cast<DictionaryEntry>() .ToDictionary(x => x.Key.ToString(), x => x.Value.ToString()); var jsonString = JsonConvert.SerializeObject(resourceSet); 
+17
source share

I liked the Karhgath solution, but I did not want to use Json.Net, since I had a list with key / value pairs. Therefore, to build the Karhgath solution, I just got stuck in the dictionary:

 public static string ToJson(ResourceManager rm) { Dictionary<string, string> pair = new Dictionary<string, string>(); ResourceSet resourceSet = rm.GetResourceSet(CultureInfo.CurrentUICulture, true, true); resourceSet.Cast<DictionaryEntry>().ToDictionary(x => x.Key.ToString(), x => x.Value.ToString()); string json = ""; foreach (DictionaryEntry item in resourceSet) { if (json != "") { json += ", "; } json += "\"" + item.Key + "\": \"" + item.Value + "\""; } return "{ " + json + " }"; } 
0
source share

All Articles