Scenario:
I have to send a Httpwebrequest and server demands it will accept only two values as json format, i want to send one more request to another server and that demands one value at a time in json format.
For above scenario i created a Class and provide all three properties like following
pubilc class MyClass
{
public string as { get; set;}
public int value { get; set;}
public string asd { get;s et;}
}
for first HttpWebRequest to the first server i want to send only two properties from MyClass 'as' and 'asd' now i will serialize through JsonConvert function of NewtonSoft as following
MyClass class = new MyClass();
string json = JsonConvert.SerializeObject(class);
the above syntx will return json having 0 and null values properties, NewtonSoft provide the functions to remove the null value from the json but it can't remove the properties having value 0 OR you can say if your property data type is int and there is no any value assigned than it assign 0 to those properties.
Syntax to remove Null properties from Json
string json = JsonConvert.SerializeObject(class, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
the above syntax will remove the null values during serialize the MyClass object.
Now Question how to remove properties from json if it has properties having 0.
No comments:
Post a Comment