我有下面的代碼:如何刪除額外字符JSON字符串
ProductDto newProduct = new ProductDto() { Id = 2, Name = "Pixel xl",Price = 2000};
然後我序列通過下面的代碼,並將其保存在數據庫:
private static string ToJson(object model)
{
JsonSerializerSettings jsonWriter = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
};
var type = model.GetType();
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (PropertyInfo item in type.GetProperties())
{
var propName = item.Name;
var propValue = item.GetValue(model);
if (propValue == null) continue;
propValue = JsonConvert.SerializeObject(propValue, Formatting.Indented, jsonWriter);
dic.Add(propName, propValue.ToString());
}
return JsonConvert.SerializeObject(dic);
}
在數據庫中的結果是象下面這樣:
{ 「ID」: 「2」, 「姓名」: 「\」 像素XL \ 「」, 「價格」: 「2000」}
所以當我讀到的字符串,它Desialize下面我有值名稱:
\"Pixel xl\"
相反:Pixel xl
我想比較那些像「這一點,但beceasue \他們不一樣。
// newValue is "Pixel xl";
// oldValue is "\"Pixel xl\"";
if (!Object.Equals(newValue, oldValue))// this line always return true
{
// do somethid if they are not same
}
你的意思是它被保存在數據庫中'\「'值? –
@JericCruz我的意思是我煥它featch從DB我不需要'\」'怎麼一回事,因爲我想將它與另一個字符串 – FullStack
比較這將永遠不會是真實的,因爲您通過引用比較它 –