0

I need to generate a JSON object as below using ASP.NET MVC 3.0 or above. All good except that some of the properties have - 'dash' e.g. scale-x. C# doesn't like this, is there a way to workaround this?

{
    "Data" : [
        {
            "scale-x":{
                "values":["1","2"],
            },
            "scale-y":{
                "line-width":"1px",
                }
            },
            "series" : [
                {
                    "values" : [2, 5]
                }
            ]
        }
    ]
}

Thanks.

Nil Pun
  • 17,035
  • 39
  • 172
  • 294
  • possible duplicate of [JavaScriptSerializer - how to deserialize a property with a dash ("-") in it's name?](http://stackoverflow.com/questions/7494280/javascriptserializer-how-to-deserialize-a-property-with-a-dash-in-its-n) – archil May 29 '12 at 07:32

3 Answers3

0

Use this as attribute for properties:

[DataMember(Name = "scale-x")] 

Also you must use serializer in System.Runtime.Serialization.Json. Look at this.

Community
  • 1
  • 1
Ilya Shpakovsky
  • 281
  • 1
  • 3
  • 16
0

Anonymous types cannot have attributes used on them, your best bet is to just define a Proxy object as your own type and use it with the serializer, then use the above answer.

whoblitz
  • 1,065
  • 1
  • 11
  • 17
0

You could use ClaySharp A good example by Hanselman is here

var person = New.Person();
person["FirstName"] = "Louis";
person["LastName"] = "Dejardin";
leon.io
  • 2,779
  • 1
  • 18
  • 26