如何在c#中解析JSON(数组数据)

时间:2014-08-21 21:25:55

标签: c# ios objective-c

我正在向asp.net发送JSON数据,但我无法读取它的内容。我想要的是我想将JSON数组值读作密钥对数据

(void)checkWithServer:(NSDictionary *)data {     //创建POST请求有效负载。

NSDictionary *tmp = [[NSDictionary alloc]initWithObjectsAndKeys:
                     @"user/login",@"routeFunc",data, @"data",
                     nil];
NSError *error;
 NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSData *postdata = [NSJSONSerialization dataWithJSONObject:tmp options:0 error:&error];

NSString *serverURL = baseURLDefault;

// Create the POST request to the server.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serverURL]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [postdata length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postdata];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"%@",[[request URL]absoluteString]);
[conn start];
[NSURLConnection sendAsynchronousRequest: request
                                   queue: queue
                       completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error) {
                           if (error || !data) {
                               NSLog(@"sonuc %@ %@",error,data);
                           } else {
                               NSError *parseError = nil;
                               NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
                               NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
                               NSDictionary *dic = [httpResponse allHeaderFields];
                               NSLog(@"%@ %@",dic,dictionary);
                               if (!dictionary) {
                                   NSLog(@"%s: JSONObjectWithData error: %@; data = %@", __FUNCTION__, parseError, [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
                                   return;
                               }
                           }
                       }
 ];
}

我试过这个但没有工作

var dict = new Dictionary<string, string>();
foreach (string key in Request.QueryString.Keys)
{
   dict.Add(key,Request.QueryString[key]);
}
string json = new JavaScriptSerializer().Serialize(dict);
Response.ContentType = "text/plain";
Response.Write(json);

0 个答案:

没有答案