2013-03-27 65 views
8

我在數據庫中有一個字段存儲一個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"] 
     }, 
     ... 
    ] 

回答

7

使用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

-1

您將需要反序列化數據。 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

相關問題