Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET C# convert ResourceSet to JSON

Tags:

json

c#

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);

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

like image 352
Martin Drapeau Avatar asked Jan 13 '14 19:01

Martin Drapeau


People also ask

What is .NET C#?

C# (pronounced "See Sharp") is a modern, object-oriented, and type-safe programming language. C# enables developers to build many types of secure and robust applications that run in . NET. C# has its roots in the C family of languages and will be immediately familiar to C, C++, Java, and JavaScript programmers.

Does .NET support C?

No. The . NET Framework is a bunch of classes(libraries) abstracting some lower-level windows functionality. C and C++ are languages.

Is .NET similar to C++?

C++ is a object oriented programming language. But here . Net is a framework which supports multiple languages to build applications using . net class libraries which will be excueted via CLR .

What is .NET language?

NET is a free, cross-platform, open source developer platform for building many different types of applications. With . NET, you can use multiple languages, editors, and libraries to build for web, mobile, desktop, games, IoT, and more.


1 Answers

Since ResourceSet is an old collection class (HashTable) and uses DictionaryEntry, you need to convert the resourceSet to a 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);
like image 120
Karhgath Avatar answered Oct 09 '22 07:10

Karhgath