.NET C#将ResourceSet转换为JSON

Mar*_*eau 7 c# json

我想从资源文件(.resx)创建一个JSON对象.我将它转换ResouceSet为如此:

ResourceSet resourceSet = MyResourceClass.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
Run Code Online (Sandbox Code Playgroud)

我现在在表单中有一组对象,{Key:<key>, Value:<value>}但更喜欢将其转换为表单中的JSON或哈希映射{Key:Value, ...}.

Kar*_*ath 19

既然ResourceSet是旧的集合类(HashTable)并使用DictionaryEntry,则需要将resourceSet转换为a Dictionary<string, string>并使用Json.Net对其进行序列化:

resourceSet.Cast<DictionaryEntry>()
           .ToDictionary(x => x.Key.ToString(),
                         x => x.Value.ToString());

var jsonString = JsonConvert.SerializeObject(resourceSet);
Run Code Online (Sandbox Code Playgroud)