c# 物件轉JSON當為null時不轉
阿新 • • 發佈:2019-01-28
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace test
{
class MainClass
{
public static void Main (string[] args)
{
EngineScoreSubWord sub = new EngineScoreSubWord ();
string json = "{\"subtext\":null,\"volume\":0.0,\"begin\":0.0,\"end\":0.0}";
sub = (EngineScoreSubWord)JsonConvert.DeserializeObject(json, typeof(EngineScoreSubWord));
string str = JsonConvert.SerializeObject(sub);
Console.WriteLine (str);
}
}
class EngineScoreSubWord
{
//[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
public string subtext { get; set; }
public float volume { get; set; }
public float begin { get; set; }
public float end { get; set; }
[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
public float? score { get; set; } //add by grq
}
}
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace test
{
class MainClass
{
public static void Main (string[] args)
{
EngineScoreSubWord sub = new EngineScoreSubWord ();
string json = "{\"subtext\":null,\"volume\":0.0,\"begin\":0.0,\"end\":0.0}";
sub = (EngineScoreSubWord)JsonConvert.DeserializeObject(json, typeof(EngineScoreSubWord));
string str = JsonConvert.SerializeObject(sub);
Console.WriteLine (str);
}
}
class EngineScoreSubWord
{
//[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
public string subtext { get; set; }
public float volume { get; set; }
public float begin { get; set; }
public float end { get; set; }
[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
public float? score { get; set; } //add by grq
}
}