我有下面的代码:如何删除额外字符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
比较这将永远不会是真实的,因为您通过引用比较它 –