簡體   English   中英

使用C#Rest Service進行NSJSONSerialization

[英]NSJSONSerialization with C# Rest Service

因此,我是一個使用C#和Web知識的完整菜鳥,並試圖弄清楚一些東西。 有一些代碼說明了這一點:

[WebInvoke(UriTemplate = "People", Method = "GET", ResponseFormat = WebMessageFormat.Json)]
public string GetPeople() {
   Person results = DataAccess.ParsePeople();
   System.WebScrip.Serialization.JavaScriptSerializer oSerializer = oSerializer.MaxJsonLength = int.MaxValue;
   string sJSON = oSerializer.Serialize(results);
   return sJSON;

}

當我為此方法輸入網址時,我的響應如下所示:

"{\"AddressesCollection\":[{\"Street\":\"1234 Temp Dr\",\"Zip\":94011},{\"Street\":\"56789 Nothing Dr\",\"Zip\":2222},\"ErrorMessage\":\"SUCCESS\"}"

我試圖在iPad上遵循此教程: http : //www.raywenderlich.com/5492/working-with-json-in-ios-5

以他們用作示例的網站為例,JSON輸出如下所示:

{“ paging”:{“ page”:1,“ total”:4440,“ page_size”:20,“ pages”:222},“ loans”:[{“ id”:447290,“ name”:“ Rosa” ,“說明”:{“語言”:[“ es”,“ en”]},“狀態”:“籌款”,“ funded_amount”:0,“ basket_amount”:0,“ image”:{“ id”: 1134583,“ template_id”:1},“活動”:“動物銷售”,“部門”:“農業”,“使用”:“購買bar和飼料。”,“位置”:{“ country_code”:“ PE”,“國家”:“秘魯”,“鎮”:“ M \\ Lambayeque”,“ geo”:{“ level”:“國家”,“ pairs”:“-10 -76”,“ type” :“ point”}},“ partner_id”:143,“ posted_date”:“ 2012-07-13T19:00:05Z”,“ planned_expiration_date”:“ 2012-08-12T19:00:05Z”,“貸款金額”:400 ,“ borrower_count”:1},{“ id”:447292,“ na

在iPad上,當我這樣做時:

NSDictionary *fields = [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&error];

要么

NSArray *fields = [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&error];

兩者都是空的。 是C#的輸出不正確的JSON嗎? 謝謝。

您的JSON字符串在某處缺少結尾[ ]

假設您發布的字符串正是服務器返回的字符串,那么您可能還需要刪除反斜杠才能使其成為有效的JSON。

NSString *responseString = [[[NSString alloc] initWithData:response] stringByReplacingOccurrencesOfString:@"\\" withString:@""];
NSDictionary *fields = [NSJSONSerialization JSONObjectWithData:responseString options:kNilOptions error:&error];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM