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!

Community
  • 1
  • 1
eestein
  • 4,914
  • 8
  • 54
  • 93
  • possible duplicate of [JSON.NET: Serialize json string property into json object](http://stackoverflow.com/questions/17584701/json-net-serialize-json-string-property-into-json-object) – MethodMan Jan 26 '15 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 – eestein Jan 26 '15 at 22:36
  • that's cool nothing wrong with trying something new or thinking outside the box.. the choice is totally yours ...:) – MethodMan Jan 26 '15 at 22:45

2 Answers2

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

eestein
  • 4,914
  • 8
  • 54
  • 93
David L
  • 32,885
  • 8
  • 62
  • 93
  • Thank you, I'll give it a go now. BRB with the results. – eestein Jan 26 '15 at 22:50
  • 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? – eestein Jan 26 '15 at 23:04
  • 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. – eestein Jan 26 '15 at 23:17
  • Perfect! Glad you were able to sort it out! – David L Jan 26 '15 at 23:26
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);
jamesSampica
  • 12,230
  • 3
  • 63
  • 85