0

Here is something i couldn't get around my head even after spending a few hours. Hoping someone will direct me.

I have a Dictionary object which I want to convert to JSON.

Sample code:

Dictionary<String,String> users = new Dictionary<String,String>();

Users look something like this:

{[name1, department1],[name2, department2]}

Here is the custom JSON format for each user:

public class User
{
    public string name;
    public string dept;
    // has get and set methods for each.
}

How can I write the users Dictionary as a JSON object of type user?

1 Answer 1

3

Ideally if the dictionary represents a collection of user objects then it in fact should be a collection of user objects. But failing that, it can easily be transformed into one:

users.Select(u => new user { name = u.Key, dept = u.Value });

The resulting enumerable can then be serialized using pretty much any serializer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.