0

I need to serialize a list of objects, but not using the "default way":

Let's say I have this class in C#:

public class Test
{
    public string Key;
    public string Value;
}
List<Test> tests;

If I serialize this list (return Json(tests.ToArray())) I get this

{"Key": "vKey1", "Value": "value1"}, {"Key": "vKey2", "Value": "value2"}

Instead of that, I want this result:

{"vKey1": "value1"}, {"vKey2": "value2"}

EDIT:

This is the desired output:

{"vKey1": "value1", "vKey2": "value2"}

I want the first variable's content to be the JS property name and the second one its value.

Any ideas? I've seen this solution:

How do I convert a dictionary to a JSON String in C#?

but I don't wanna transform my object list into a dictionary so I could transform it again using that string.format solution.

Thanks!

3
  • possible duplicate of JSON.NET: Serialize json string property into json object Commented Jan 26, 2015 at 22:22
  • @MethodMan I understand why you think this way, but I'm not sure I agree. After studying the proposed problem/solution the other OP has a string inside a property he wants to be separately serialized and his string is already in a json format, whereas I have a whole class with two properties (could be done in a linq select as well) and I want those properties to be serialized that way Commented Jan 26, 2015 at 22:36
  • that's cool nothing wrong with trying something new or thinking outside the box.. the choice is totally yours ...:) Commented Jan 26, 2015 at 22:45

2 Answers 2

1

If you are using JSON.Net (and I assume you are since you are using MVC 5), you can transform your list into a

List<Dictionary<string, string>>

Each list entry should be a new dictionary with one entry in that dictionary. JSON.Net will replace the property name with your dictionary key value, giving you the structure that you need.

public ActionResult Test()
{
    tests = new List<Test>
    {
        new Test {Key = "vKey1", Value = "value1"},
        new Test {Key = "vKey2", Value = "value2"}
    };

    var tests2 = new List<Dictionary<string, string>>();

    tests.ForEach(x => tests2.Add(new Dictionary<string, string>
    {
        { x.Key, x.Value }
    }));

    return Json(tests2, JsonRequestBehavior.AllowGet);
}

Produces the following JSON:

[{"vKey1":"value1"},{"vKey2":"value2"}]

EDIT:

To reflect the desired solution:

tests.ForEach(x => tests2.Add(x.Name, x.Value));

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

4 Comments

Thank you, I'll give it a go now. BRB with the results.
Err, I'm sorry, I gave the wrong output. it's not supposed to be returned as an array, but a object with properties: {"vKey1":"value1", "vKey2":"value2"}. I'm accepting your answer because it does answer my question, I was wrong in my desired output. I'm trying to think on your solution, do you think it's possible to merge this array and turn it into an object?
Never mind, my bad for not paying attention to your code... this is the solution I was looking for: tests.ForEach(x => tests2.Add(x.Name, x.Value)); Thanks again.
Perfect! Glad you were able to sort it out!
1

This is a more generalized approach without the need for a List (just an IEnumerable).

var tests = new List<Test>
{
    new Test {Key = "vKey1", Value = "value1"},
    new Test {Key = "vKey2", Value = "value2"}
};

var dict = tests.ToDictionary(t => t.Key, t => t.Value);

return Json(dict);

1 Comment

Indeed it is! Thanks!

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.