0

I am working on a C# project however I am in need of some advice.

I am presently posting to my site:

{Tags : 'App', Limit : '10' }

And it can cast this to the following class

[Serializable]
public class MiloFilter
{
    public string Tags { get; set; }
    public string Limit { get; set; }
}

However what I am wanting to accomplish is that I would like to post my JSON like this:

{ MiloFilter : {Tags : 'SomeTag', Limit : '1' }}

However when I try and parse it using the following method it fails.

var js = new System.Web.Script.Serialization.JavaScriptSerializer();
var miloFilter = js.Deserialize<MiloFilter>(bodyText);

How can I acomplish this?

Mironline
  • 2,755
  • 7
  • 35
  • 61
TheMonkeyMan
  • 8,622
  • 8
  • 27
  • 42

1 Answers1

1

You can easily create your own serializer

var car = new Car() { Name = "Ford", Owner = "John Smith" };
string json = Serialize(car);

string Serialize<T>(T o)
{
    var attr = o.GetType().GetCustomAttribute(typeof(JsonObjectAttribute)) as JsonObjectAttribute;

    var jv = JValue.FromObject(o);

    return new JObject(new JProperty(attr.Title, jv)).ToString();
}

source

Community
  • 1
  • 1
Mironline
  • 2,755
  • 7
  • 35
  • 61
  • OP is using [`JavaScriptSerializer`](http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer%28v=vs.110%29.aspx) not [`Json.NET`](http://james.newtonking.com/json). – dbc Nov 18 '14 at 09:43