Handle null Json Value in C. How to handle it here

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I have gone through some of the post regarding this but couldn t get the solution. How to handle the null value here in the following code?

 foreach (KeyValuePair<string, object> obj in mainList[i])
 {
     PList[i].Add(obj.Value.ToString());
 }

I am getting null value (null reference exception) while trying to get object value in the list. I have tried something like this,

foreach (KeyValuePair<string, object> obj in mainList[i])
{
   try
   {
       var check = obj.Value.ToString();
       PList[i].Add(check);
   }
   catch(NullReferenceException)
   {
       var check = "Null";
       PList[i].Add(check);
   }
}

I can achieve my target using the second snippet (using try catch blocks) but it seems to be very slow. It takes almost 30 seconds to process this for loop. So is there any other way on how to handle this null Json value?

mainList = List<Dictionary<String,String>>.
PList = List<String>.

Answers

You can check if value is null before add it to list.

foreach (KeyValuePair<string, object> obj in mainList[i])
{
    PList[i].Add(obj.Value == null ? "Null" : obj.Value.ToString());
}

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/25092595/handle-null-json-value-in-c-how-to-handle-it-here

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils