Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ASP.NET web服务上解析JSON字符串,并返回JSON字符串_Asp.net_Iphone_Json_Service - Fatal编程技术网

在ASP.NET web服务上解析JSON字符串,并返回JSON字符串

在ASP.NET web服务上解析JSON字符串,并返回JSON字符串,asp.net,iphone,json,service,Asp.net,Iphone,Json,Service,Hy 我是一名iPhone开发人员,我有一个应用程序,它必须与asp.net和mssql数据库进行通信。我想,用JSON字符串通信就可以了。我已经实现了iPhone端,但在那之后,我就卡住了。 -如何将JSON字符串解析为.NET类? -如何使用数据调用存储过程? -如何以JSON字符串响应iPhone 以下是我的实现: MutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [

Hy

我是一名iPhone开发人员,我有一个应用程序,它必须与asp.net和mssql数据库进行通信。我想,用JSON字符串通信就可以了。我已经实现了iPhone端,但在那之后,我就卡住了。 -如何将JSON字符串解析为.NET类? -如何使用数据调用存储过程? -如何以JSON字符串响应iPhone

以下是我的实现:

    MutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"**POST**"];
    [request setCachePolicy:NSURLCacheStorageNotAllowed];
    [request setTimeoutInterval:60.0f];
    SBJsonWriter *writer = [SBJsonWriter new];
    NSString *paramStr = [writer stringWithObject:_parameters];
    [writer release];
    NSData *requestData = [paramStr dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

    [request setHTTPBody:requestData];
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
以下是包含请求和有效负载(requestData)的日志:

使用类的
反序列化
方法将JSON解析为.NET类(.NET 3.5 SP1和4.0)。 通过将类的属性作为参数传递给过程来调用存储过程

使用类的
Serialize
方法将.NET对象转换为JSON


或者使用WCF服务,它将为您处理一些序列化。

我喜欢使用带有ScriptService属性(System.web.Script.Services命名空间)修饰的ASP.NET web服务(.asmx文件)。这将自动将传入的JSON对象转换为.NET类,结果将转换为JSON:

public class UserInfo
{
   public string password { get; set; }
   public string email { get; set; }
}

public class ServiceResult
{
   // Service result
}

[ScriptService]
public class MyService
{
   [WebMethod, ScriptMethod(UseHttpGet = false)]
   public ServiceResult DoStuff(UserInfo userInfo)
   {
      // Do stuff with userInfo.password, userInfo.email
      return new ServiceResult();
   }
}

我愿意花大把的钱去看一个简单的C#webservice与Android的应用程序发明家TinyWebDB GetValue和StoreValue方法进行通信。我不清楚的是,我的值标记是否需要在服务的代码中定义(硬编码),或者它们是否可以在参数中发送到web服务。
public class UserInfo
{
   public string password { get; set; }
   public string email { get; set; }
}

public class ServiceResult
{
   // Service result
}

[ScriptService]
public class MyService
{
   [WebMethod, ScriptMethod(UseHttpGet = false)]
   public ServiceResult DoStuff(UserInfo userInfo)
   {
      // Do stuff with userInfo.password, userInfo.email
      return new ServiceResult();
   }
}