How do I convert an IEnumerable to JSON?

IEnumerable<int> sequenceOfInts = new int[] { 1, 2, 3 };
IEnumerable<Foo> sequenceOfFoos = new Foo[] { new Foo() { Bar = "A" }, new Foo() { Bar = "B" } };

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string outputOfInts = serializer.Serialize(sequenceOfInts);
string outputOfFoos = serializer.Serialize(sequenceOfFoos);

Which produces the output

[1,2,3]
[{"Bar":"A"},{"Bar":"B"}]

And then you can get your sequence back

IEnumerable<Foo> foos = serializer.Deserialize<IEnumerable<Foo>>(outputOfFoos);

Leave a Comment

techhipbettruvabetnorabahisbahis forumu