Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert null strings to empty string json.net

Tags:

c#

json.net

I'm using json.net to serialize a class that has strings, however, when it is converted to json the strings are convert to null, is there a way I could make json.net convert null strings to emtpy strings ('') and not null?

Here is what I currently get

var client = {
"FirstName": null,
"LastName": null
}

and I want this:

var client = {
 "FirstName": '',
 "LastName": ''
}
like image 611
ryudice Avatar asked Mar 15 '11 05:03

ryudice


1 Answers

try:

client.FirstName||''

This will return '' if FirstName is null. Better still create a helper function like this:

function null2empty(a){
    return a||'';//You might want to check for strings only before returning
}
like image 189
TheVillageIdiot Avatar answered Oct 20 '22 19:10

TheVillageIdiot