0

I have a class

public class MyClass
{
    public string v1 { set; get; }
    public string v2 { set; get; }
    public string v3 { set; get; }
    public string i1 { set; get; }
    public string i2 { set; get; }
    public string i3 { set; get; }
    public string error { set; get; }
    public string date { set; get; }
}

when i Serialize the class

 MyClass meter = new MyClass();
 var json = new JavaScriptSerializer().Serialize(meter);

i get

{
    "v1":"2342",
    "v2":"2336",
    "v3":"2332",
    "i1":"38.90",
    "i2":"42.21",
    "i3":"30.87",
    "error":"",
    "date":"26/02/2015 08:16:14"
}

how can i change the display name of every member (like [ScriptIgnore])

i tried [Display(Name = "myname")]

eyalb
  • 2,994
  • 9
  • 43
  • 64

1 Answers1

1

There are two ways

1) Use the Newtonsoft.Json and just add the property name to display There are this way

 [JsonProperty(PropertyName = "owner")]
 public string Owner { get; set; }

2) If you stick with javascriptserializer then here is the code to how to do it. JavaScriptSerializer.Deserialize - how to change field names

I think you should use the Newtonsoft why reinvent the wheel??

Community
  • 1
  • 1
Mahesh
  • 8,694
  • 2
  • 32
  • 53
  • is there a function for it to force it to use the class object's property name instead of the JSON object's properties? – aj go Apr 30 '21 at 12:45