【问题标题】:Deserialising JSON with HTML c#使用 HTML c# 反序列化 JSON
【发布时间】:2014-06-27 05:59:19
【问题描述】:

我正在将一个对象序列化为 JSON,其中包含一个包含 HTML 的属性

所以我的班级如下所示。

[Serializable]
public class ProductImportModel
{
  public string ProductName { get; set; }
  public string ShortDescription { get; set; }
}

所以记录看起来像

 ProductName = "Test Product";
 ShortDescription = "<p>TEST Product</p>";

我认为使用实体框架将其存储到数据库中

 var syncProduct = new SyncProduct();
 syncProduct.ProductId = Guid.NewGuid();
 syncProduct.SerializedProduct = Newtonsoft.Json.JsonConvert.SerializeObject(importProduct);

如果我查看数据库,它看起来很好

  {"ProductName":"Test Product","ShortDescription":"<p>TEST Product</p>"}

然后我想使用实体框架检索项目,它删除 HTML 标记

  var jsonProduct = context.SyncProducts.FirstOrDefault();

  jsonProduct.SerializedProduct 

  {"ProductName":"Test Product","ShortDescription":"pTEST Productp"}

我认为这是实体框架中的某种编码问题。 我应该如何检索它?

【问题讨论】:

  • 您应该在保存到数据库时对其进行 HtmlEncode,然后在检索时对其进行解码。
  • 如果 DB 中的 SerializedProduct-column 是 varchar 应该没有问题。是吗?

标签: c# html json entity-framework serialization


【解决方案1】:

更改 Json.net 的版本后,它现在似乎可以工作了。应该是版本的bug吧。

【讨论】:

    猜你喜欢
    • 2011-05-30
    • 2011-10-06
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    相关资源
    最近更新 更多