简体   繁体   中英

How to refine json by remove null values properties and zero (0) value properties from the c# object

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 the 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 syntax will return json having 0 and null values properties, NewtonSoft provide the functions to remove the null value from 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.

您可以尝试将 int 属性定义为可空:

public int? value { get; set;}

Just modify your expression

string json = JsonConvert.SerializeObject(
  class, 
  Newtonsoft.Json.Formatting.Indented, 
  new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }
);

as below,

string json = JsonConvert.SerializeObject(
  class, 
  Newtonsoft.Json.Formatting.Indented, 
  new JsonSerializerSettings { 
    NullValueHandling = NullValueHandling.Ignore,
    DefaultValueHandling = DefaultValueHandling.Ignore 
  }
);

Answer given by mgigirey accepted by me, Another solution which i did, I converted my Json into xml and removed the nodes having value 0 and again converted into Json. My solution is also working for me but it is bit lengthy and slower then Mgigirey answer.

So two answers are here, if you anyone want to go with my solution then see following.

//This line will remove the null as i earlier mentioned in my question.
string jsonStatus = JsonConvert.SerializeObject(myJson, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

//Create xml object by covnert json into xml
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);

//Get the property name which have the value 0
var v = doc.GetElementsByTagName("value")[0];

//Remove the child node.
doc.DocumentElement.RemoveChild(v);

//Again convert into the json.
string jsonText = JsonConvert.SerializeObject(doc);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM