2013-11-04 109 views
0

我以JSON格式獲取響應。我使用jsSerializer從JSON獲取字段的值。它在大部分工作,但我不知道爲什麼在一種情況下回來JSON已在其中\n,並因此,jsSerializer無法正常工作。在換行符上解析JSON失敗

這是我的代碼:

protected void btnStats_Click(object sender, EventArgs e) 
    { 

     IRestResponse response = GetStats();//Stats provide you with the summary of the events that occur with your messages. 

     JavaScriptSerializer jsSerializer = new JavaScriptSerializer(); 

     var jsobj = jsSerializer.Deserialize<dynamic>(response.Content); 


     int total_count= Convert.ToInt32(jsobj[0]["total_count"]); //I am getting error 

,這是返回JSON的方法:

public static IRestResponse GetStats() 
    { 
     RestClient client = new RestClient(); 
     client.BaseUrl = "https://api.mailgun.net/v2"; 
     client.Authenticator = 
       new HttpBasicAuthenticator("api", 
              "key"); 
     RestRequest request = new RestRequest(); 
     request.AddParameter("domain", 
          "messenger.test.com", ParameterType.UrlSegment); 
     request.Resource = "{domain}/stats"; 
     request.AddParameter("event", "sent"); 
     request.AddParameter("event", "opened"); 
     request.AddParameter("skip", 1); 
     request.AddParameter("limit", 2); 
     return client.Execute(request); 
    } 

我已經sevaral這些方法正在工作的罰款,唯一不同的是我發現的是(response.Content)中的JSON格式有\n。我沒有做任何事情來刪除\n;它只是不在那裏。

這是我從該代碼獲取JSON:

"{\n \"total_count\": 54,\n \"items\": [\n {\n  \"total_count\": 715,\n  \"created_at\": \"Mon, 04 Nov 2013 00:00:00 GMT\",\n  \"tags\": {},\n  \"id\": \"5276e3835b8917d8268a6df1\",\n  \"event\": \"opened\"\n },\n {\n  \"total_count\": 688,\n  \"created_at\": \"Sun, 03 Nov 2013 00:00:00 GMT\",\n  \"tags\": {},\n  \"id\": \"527592035b8917d8268a1348\",\n  \"event\": \"sent\"\n }\n ]\n}" 

,並在工作正常的另一種方法,我得到這樣的JSON的:

"[{\"unique\": {\"link\": 1}, \"total\": 35, \"recipient\": \"[email protected]\"}, {\"unique\": {\"link\": 1}, \"total\": 22, \"recipient\": \"[email protected]\"}]" 
+0

如果您的JSON解析器無法處理空白,則可能需要新的JSON解析器。無論如何,你說有一個錯誤 - 它是什麼? – Ryan

+0

錯誤是'System.Collections.Generic.Dictionary .this [string]'的最好的重載方法匹配有一些無效的參數,這是因爲它找不到該feild。 – Alma

回答

1

格式化JSON字符串可以在其中有新行和其他空白格式。這將在Visual Studio調試器中顯示爲\n

它將解析相同的JSON字符串沒有額外的空白。

錯誤出現在您的代碼中,而不是JSON中。您正試圖在動態值上使用索引器。但動態值不是一個數組,因此它不支持索引器。這是一個對象。您必須手動訪問屬性,如:

jsobj["total_count"]

你列出的第一個JSON字符串是與「項目」子陣列的對象。因此,您可以編寫jsobj["items"][0]以訪問該數組中的第一個對象(如果至少有一個對象)。

您發佈的第二個JSON字符串不是包裝在對象中的數組,它只是一個數組。

您可能想要反序列化爲強類型對象而不是動態類型。因爲它沒有驗證JSON的格式符合您的期望,所以使用動態變得更加簡單。如果您的JSON格式錯誤,您希望在反序列化期間發生錯誤,而不是稍後的某個時間點。

因此我將創建類似下面的對象:

public class SomeResult 
{ 
    public int total_count { get; set; } 
    public List<SomeItem> items { get; set; } 
} 

public class SomeItem 
{ 
    public int total_count { get; set; } 
    public DateTime created_at { get; set; } 
    ... 
} 

然後,你可以寫:

SomeResult result = serializer.Deserialize<SomeResult>(response.Content);

現在你有一個強類型類,您可以訪問所有的結果。

請記住,名稱需要完全匹配,並且您使用的類型必須與反序列化的值類型匹配。例如,如果DateTime有可能爲空,則必須使用可空的DateTime(DateTime?)。

+0

問題是,當它有\ n JavaScriptSerializer不工作,所以我不能在這一行中得到json feild的值:int total_count = Convert.ToInt32(jsobj [0] [「total_count」]); – Alma

+0

因此,如何訪問以下內容:當我使用以下字符串時:string daily = jsobj [「items」] [「0」];如何訪問此項:{ 「items」:[ 「total_count」:678, – Alma

+0

它給了我錯誤,不能隱式地將字符串轉換爲int,當我轉換爲int時,它給了我一個錯誤,不能隱式地將int轉換爲字符串。 – Alma