2016-09-19 42 views
2

我是新入json.net,所以任何幫助將不勝感激。Json.net忽略子對象的空值

我使用的是json.net的net 2.0版本,因爲我需要將它集成到Unity3d引擎中建議的答案(使用nullvaluehandling)不起作用!我應該認爲這是最舊版本的恢復嗎?

所以,一個有一個這樣的對象:

Object() 
{ 
    ChildObject ChildObject1; 
    ChildObject ChildObject2; 
} 

ChildObject() 
{ 
    Property1; 
    Property2; 
} 

我想json.net到連載只有所有對象的不空的屬性,包括子對象。所以,如果我instatiate僅ChildObject1和ChildObject2的property2的property1,從JsonConvert字符串會是這樣:

{ 
    "ChildObject1": 
     { 
      "Property1": "10" 
     }, 
    "ChildObject1": 
     { 
      "Property2":"20" 
     } 
} 

但是默認行爲這樣造成字符串:

{ 
    "ChildObject1": 
      { 
      "Property1": "10", 
      "Property2": "null" 
      }, 
    "ChildObject2": 
      { 
      "Property1": "null, 
      "Property2":"20" 
      } 
} 

我知道NullValueHandling,但在我的情況下,這不正常工作!它只忽略父對象(我們序列化的對象)的空屬性,但如果這個父對象有一些子對象,它將不會忽略這個子對象的空屬性。我的情況有所不同。

如果即使子對象的一個​​屬性不爲空,它也會將其序列化爲一個具有一個非空屬性的字符串,其他值爲空。我的代碼


UPD例如:

我已經嵌套類

我的目標是這樣的:

public class SendedMessage 
{ 
    public List<Answer> Answers { get; set; } 
    public AnswerItem Etalon {get; set;} 
} 

public class Answer() 
{ 
public AnswerItem Item { get; set; } 
} 

    public class AnswerItem() 
    { 
    public string ID { get; set; } 
    public bool Result { get; set; } 
    public int Number { get; set; } 
    } 

我instatiate一個SendedMessage對象是這樣的:

new SendedMessage x; 
x.Etalon = new AnswerItem(); 
x.Etalon.Result = true; 

比我使用

string ignored = JsonConvert.SerializeObject(x, 
      Formatting.Indented, 
      new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); 

,它使這個字符串:

{ 「標準具」:{ 「ID」= 「空」, 「結果」= 「假」,號碼」 = 「空」} }

所以它真的忽略了空對象的答案(一個List),但是不要忽略子對象的空屬性。

感謝您的幫助!

+1

的可能的複製[如何忽略類的屬性,如果爲空,使用json.net(HTTP://計算器.com/questions/6507889/how-to-ignore-a-property-in-class-if-null-using-json-net) – JeetDaloneboy

+0

參考:http:// stackoverflow。com/questions/33027409/json-net-serialize-jobject-while-ignoring-null-properties – JeetDaloneboy

+0

好吧,不,這是不同的情況。 NullvalueHandling.Ignore **不忽略序列化對象的子對象**的空屬性。 – Rodoleia

回答

3

使用本:

string ignored = sonConvert.SerializeObject(movie, 
     Formatting.Indented, 
     new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); 

這裏是例子:

public class Movie 
{ 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public string Classification { get; set; } 
    public string Studio { get; set; } 
    public DateTime? ReleaseDate { get; set; } 
    public List<string> ReleaseCountries { get; set; } 
} 

static void Main(string[] args) 
{ 
    Movie movie = new Movie(); 
    movie.Name = "Bad Boys III"; 
    movie.Description = "It's no Bad Boys"; 

    string included = JsonConvert.SerializeObject(movie, 
     Formatting.Indented,new JsonSerializerSettings { }); 

      // { 
      // "Name": "Bad Boys III", 
      // "Description": "It's no Bad Boys", 
      // "Classification": null, 
      // "Studio": null, 
      // "ReleaseDate": null, 
      // "ReleaseCountries": null 
      // } 

      string ignored = JsonConvert.SerializeObject(movie, 
       Formatting.Indented, 
       new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); 

      // { 
      // "Name": "Bad Boys III", 
      // "Description": "It's no Bad Boys" 
      // } 
     } 
+0

來到你也可以把數據屬性上你想要的屬性(忽略) –

+0

@JeetDaloneboy,我現在正在使用這種方法,它會導致問題,使我回答這個問題。 NullValueHandling.Ignore僅忽略完全爲null的子對象。如果在我正在序列化的對象中有一個子對象,並且該子對象有10個屬性,並且只有其中一個屬性不爲null,則它將導致具有9個空值且只有一個有意義的屬性的字符串。我希望json忽略嵌套的空屬性。 – Rodoleia

+0

你能分享你的代碼嗎? – JeetDaloneboy