2015-04-21 67 views
-2

引用數組和記錄序列化時我與Newtonsoft的Json.net對象,我得到:如何序列化對象沒有json.net

{"status":"1", 
"message":"test", 
"records":"[{\"id\":\"1\", \"name\":\"file1\"}, 
      {\"id\":\"2\", \"name\":\"file2\"}, 
      {\"id\":\"3\", \"name\":\"file3\"}]" // I want to get rid of the extra quotes for the array 
} 

我想有:

{"status":"1", 
"message":"test", 
"records":[{"id":"1", "name":"file1"}, 
      {"id":"2", "name":"file2"}, 
      {"id":"3", "name":"file3"}] // NOTE: this is an Array of records 
} 

這是簡化的代碼我使用來序列:

QHttpResponse tempResponse = new QHttpResponse() { StatusCode = (int)HttpStatusCode.OK, Message = "File found." }; 
       JObject jo = JObject.FromObject(tempResponse); 
       jo.Add("records",JsonConvert.SerializeObject(jo)); 

這是QHttpResponse類:

public class QHttpResponse 
{ 

    #region Feilds 
    /// <summary> 
    /// Status code of the http response (e.g.:400 = bad request.) 
    /// </summary> 
    [JsonProperty("status_code")] 
    public int StatusCode { get; set; } 

    /// <summary> 
    /// Message (Content) of the response. 
    /// </summary> 
    [JsonProperty("message")] 
    public string Message { get; set; } 

    #endregion 

} 
+0

結帳這[JSONLint網站](http://jsonlint.com/)在這裏你驗證有效或無效的JSON蜇 – MethodMan

+1

它看起來像你有一個小''jo'添加到'jo'的錯字 – ohmusama

+0

@ohmusama:是的,我也掛了... –

回答

3

你的問題是在這裏:

jo.Add("records",JsonConvert.SerializeObject(jo)); 

序列化的陣列將它添加到「記錄」屬性,那麼這樣連載了整個事情,你得到一個雙串行化,這就是爲什麼你有逃脫「。

嘗試:

jo["records"] = arrayData; 

再後來,當你序列化這應該站出來爲你期望的那樣。

相關問題