简体   繁体   English

使用C#Rest Service进行NSJSONSerialization

[英]NSJSONSerialization with C# Rest Service

So I'm a complete noob with C# and web stuff and trying to figure something out. 因此,我是一个使用C#和Web知识的完整菜鸟,并试图弄清楚一些东西。 There is some code that says this: 有一些代码说明了这一点:

[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;

} }

When I type in the url for this method, my response looks like: 当我为此方法输入网址时,我的响应如下所示:

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

I was trying to follow this tutorial on the iPad side: http://www.raywenderlich.com/5492/working-with-json-in-ios-5 我试图在iPad上遵循此教程: http : //www.raywenderlich.com/5492/working-with-json-in-ios-5

Looking at the website they used as an example, the JSON output looks like: 以他们用作示例的网站为例,JSON输出如下所示:

{"paging":{"page":1,"total":4440,"page_size":20,"pages":222},"loans":[{"id":447290,"name":"Rosa","description":{"languages":["es","en"]},"status":"fundraising","funded_amount":0,"basket_amount":0,"image":{"id":1134583,"template_id":1},"activity":"Animal Sales","sector":"Agriculture","use":"to buy barnyard fowl and feed.","location":{"country_code":"PE","country":"Peru","town":"M\órrope - Lambayeque","geo":{"level":"country","pairs":"-10 -76","type":"point"}},"partner_id":143,"posted_date":"2012-07-13T19:00:05Z","planned_expiration_date":"2012-08-12T19:00:05Z","loan_amount":400,"borrower_count":1},{"id":447292,"na {“ 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

On the iPad, when I do: 在iPad上,当我这样做时:

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

or 要么

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

both are empty. 两者都是空的。 Is it the output of the C# that is not proper JSON? 是C#的输出不正确的JSON吗? Thanks. 谢谢。

Your JSON string is missing a closing ] somewhere. 您的JSON字符串在某处缺少结尾[ ]

Assuming the string you posted is exactly what was returned by the server, you will also probably need to remove the backslashes in order to make it valid 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