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")]

1 Answer 1

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??

Sign up to request clarification or add additional context in comments.

1 Comment

is there a function for it to force it to use the class object's property name instead of the JSON object's properties?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.