Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a slight situation. I'm interacting with a web service using RestSharp, where the service is requiring me to send the following as part of the request:

{
    "a":"a value",
    "b":"b value"
}

Which is all fine and dandy, because you could simply use a class such as this:

public class MyClass
{
    public string A { get; set; }
    public string B { get; set; }
}

However, I do not know know the property names at runtime. Therefore, I attempted to use an ExpandoObject, but of course, this simply serialized as a JSON array:

[
    "a":"a value",
    "b":"b value"
]

So, it would seem that I need to be able to serialize (and deserialize) a Dictionary (or IEnumerable<KeyValuePair<string, string>>) as a JSON object (in other words, use curly braces instead of a brackets).

Does anyone know how I might do this, preferably by using a Json.NET attribute, such that the functionality may be reused elsewhere?

share|improve this question
    
this was asked here stackoverflow.com/questions/3739094/… –  Anthony Johnston Apr 17 '12 at 22:15
    
That posters question was about the details of how to serialize a dictionary of complex objects into a Json array of Json objects. My question is about how to serialize a dictionary of simple key value pairs into a single Json object: where each key is a property name, and each value is a property value. –  Christopher Harris Apr 17 '12 at 22:19

2 Answers 2

up vote 2 down vote accepted

how about using a JObject?

var obj = new JObject();

obj["One"] = "Value One";
obj["Two"] = "Value Two";
obj["Three"] = "Value Three";

var serialized = obj.ToString(Formatting.None);

gives you

{"One":"Value One","Two":"Value Two","Three":"Value Three"}
share|improve this answer
    
It's always the simple things that get you, right? Am I the only one? I'll test this and get back to you... :) –  Christopher Harris Apr 17 '12 at 22:33
    
This worked exactly as I needed it to. Since I wanted to keep my serialization code separate from my domain, I simply looped over the key value pairs I wanted to serialize as an object, and assigned each value to the corresponding JObject's index. –  Christopher Harris Apr 18 '12 at 6:01
    
While this wasn't the Attribute solution I was looking for, one could probably adapt some solution from this, using a custom converter. –  Christopher Harris Apr 18 '12 at 6:02
1  
This suddenly stopped working for the newest version of Json.NET and Rest#... –  Christopher Harris May 31 '12 at 20:07
    
Agreed with @ChristopherHarris, I can't deserialize using this (or ExpandoObject) -- I just get empty objects back –  ashes999 Apr 22 '13 at 14:52

Use JavascripSerializer object from .net class libs. It supports reflection on the object it is serializing

see msdn docs

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.