JSON我已經創造了.NET一個簡單的.asmx Web服務,如下圖所示:修改.NET的.asmx Web服務返回的不是XML
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Foo(string name)
{
var customer = new {Name = name};
JavaScriptSerializer json = new JavaScriptSerializer();
return json.Serialize(customer);
}
當我把這個服務將返回XML而不是JSON的結果。我的iOS客戶端有一個需要JSON格式的字典。
如何讓.NET服務返回JSON格式而不是XML?
字典永遠不會被填充,因爲.Net服務返回XML而不是JSON。
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSLog(@"status code %d",request.responseStatusCode);
if(request.responseStatusCode == 200)
{
NSString *responseString = [request responseString];
NSDictionary *responseDict = [responseString JSONValue];
NSLog(@"%@",[responseDict objectForKey:@"name"]);
}
NSLog(@"request finished");
}
我也設置了內容類型,如下圖所示:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addRequestHeader: @"Content-Type" value:
@"application/json; charset=utf-8"];
[request setPostValue:@"mama" forKey:@"name"];
[request setDelegate:self];
[request startAsynchronous];
發回的回覆我得到的是這樣的:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">{"Name":"mama"}</string>
僅供參考,根據您陳述的目標,iOS代碼與此問題的解決方案無關。您的.NET服務ASMX或WCF? – Jeff
ASMX服務... – azamsharp
請參閱更新的帖子! – azamsharp