如何序列化原始json字段?

问题描述:

我在数据库中有一个字段存储一个json字符串 ,我希望当我在json结果中返回它时,它将作为json原始数据返回并且不会以引号作为字符串进行扭曲。如何序列化原始json字段?

更新1(详细信息): 如果你在看图像领域它包含一个原始JSON字符串值
但随着JsonResult序列化后得到扭曲加上引号,它确定,因为是一种类型的字符串, 我如何告诉序列化程序将图像字段视为原始json数据?

 var db = new ModelsContainer(); 
     var res = db.Images.OrderByDescending(i=>i.DateCreated).Skip(skip).Take(take).Select(i => new { 
      id = i.Id, 
      dateCreated = i.DateCreated, 
      images = i.Images , 
      user = new { 
       id = i.User.Id, 
       facebookId = i.User.FacebookId, 
       displayName = i.User.DisplayName 
      }, 
      tags = i.Tags.Select(t => t.Value) 
     }).ToList(); 

     return Json(res, JsonRequestBehavior.AllowGet); 

    [ 
     { 
      "id":"5c528e88-f3a7-4b30-9746-980867325fd1", 
      "dateCreated":"\/Date(1364381593000)\/", 
      "images":"[{\"source\":\"http://localhost:9242/images/f4956702/6d34/42db/b28a/397d0eaf3097.jpg\",\"width\":237,\"height\":237},{\"source\":\"http://localhost:9242/images/87d47041/1522/4d10/9325/105851aae259.jpg\",\"width\":633,\"height\":633},{\"source\":\"http://localhost:9242/images/2a639272/9067/42fb/83ee/e88f0a0878f8.jpg\",\"width\":547,\"height\":547},{\"source\":\"http://localhost:9242/images/37caa7b2/e183/4efc/96eb/487e556501b2.jpg\",\"width\":1024,\"height\":1024}]", 
      "user":{"id":"ea39616d-6ff9-424b-b99b-7bee53e674bb","facebookId":"608215901","displayName":"Yonathan Garti"}, 
      "tags":["test","test","test"] 
     }, 
     ... 
    ] 

使用Json.net,您可以定义自己的JsonConverters以应用特定的序列化行为。您可以将其应用于特定的类型,或者如果您有视图模型,则可以将其应用于特定的属性。

在你的情况下,你想用JsonWriter.WriteRawValue作为一个原始字符串写图像字符串。

即,

public class PlainJsonStringConverter : Newtonsoft.Json.JsonConverter 
{ 
    public override bool CanConvert(Type objectType) 
    { 
     return objectType == typeof(string); 
    } 
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     return reader.Value; 
    } 
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
     writer.WriteRawValue((string)value); 
    } 
} 

public class MyViewModel 
{ 
    public string id { get; set; } 
    [Newtonsoft.Json.JsonConverter(typeof(PlainJsonStringConverter))] 
    public string images { get; set; } 
    /* ... */ 
} 
+0

你的解决方案非常棒!但不幸的是我不能实现它,因为我的类是由实体框架生成的。有没有办法告诉序列化器关于一个字段需要一个类型转换器而不是使用属性? – ygaradon 2013-03-27 17:52:46

+2

另一种解决方案是如果你有一个自定义类型,JsonConverter处理,但在你的情况下,我相信它是一个普通的字符串,所以不会工作。我的建议是创建一个自定义View模型。在上面的代码中,只需要使用该自定义视图模型而不是初始化的匿名对象(res)。 – 2013-03-27 18:27:26

+0

天才!我怎么没有看到,我只需要用一些模型替换匿名类型,然后我就可以应用属性!感谢男人。 – ygaradon 2013-03-27 18:53:42

您将需要反序列化数据。 C#提供了一个类来处理JSON数据。

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx

http://msdn.microsoft.com/en-us/library/bb412179.aspx摘录:

通常情况下,JSON序列化和反序列化会自动被Windows通讯基础(WCF)当您使用服务运营数据合同类型暴露在支持AJAX的端点处理。但是,在某些情况下,您可能需要直接使用JSON数据 - 这是本主题演示的场景。

//Deserialize the JSON-encoded data into a new instance of Person by using the ReadObject method of the DataContractJsonSerializer. 

stream1.Position = 0; 
Person p2 = (Person)ser.ReadObject(stream1); 

//Show the results. 

Console.Write("Deserialized back, got name="); 
Console.Write(p2.name); 
Console.Write(", age="); 
Console.WriteLine(p2.age); 
+0

我不认为你理解我的问题请阅读我的更新谢谢! – ygaradon 2013-03-27 14:56:30