iOS:将以下JSON的字符串数组发布到Web服务 [英] iOS : Post array of strings to web service for the following JSON

查看:182
本文介绍了iOS:将以下JSON的字符串数组发布到Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 如何从postwebservice获得以下响应。

  2. 我有一个名为textArray的字符串数组。
    如何在以下回复中将密钥@答案的textArray发布。

  1. How to get the following response from a "post" webservice.
  2. I have an array of strings called textArray. How to post that textArray for the key @"Answer" in the following response.

      {
        "ProjID": "78",
        "Uid": "12",
        "EmailID": "ratnam_nv@yahoo.com",
        "ProjectInviterFQAnswers": [{
            "slno": "1",
            "Answer": "a1",
            "order": "1",
            "flag": "F"
        }, {
            "slno": "2",
            "Answer": "a1",
            "order": "2",
            "flag": "F"
        }, {
            "slno": "1",
            "Answer": "a1",
            "order": "2",
            "flag": "Q"
        }
        ]
    };


这是我到目前为止尝试的内容

Here's what I tried so far

NSError *error = Nil;
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];


NSDictionary *dictionaryArray1 = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"slno", @"a1", @"Answer", @"1", @"order", @"F", @"Flag", nil];

NSDictionary *dictionaryArray2 = [NSDictionary dictionaryWithObjectsAndKeys:@"2", @"slno", @"a1", @"Answer", @"1", @"order", @"F", @"Flag", nil];

NSDictionary *dictionaryArray3 = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"slno", @"a1", @"Answer", @"2", @"order", @"Q", @"Flag", nil];

NSArray *arrayAnswer = [NSArray arrayWithObjects:dictionaryArray1, dictionaryArray2, dictionaryArray3, nil];

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"78", @"ProjID", @"12", @"UID", @"ratnam_nv@yahoo.com", @"EmailID", arrayAnswer, @"ProjectInviterFQAnswer", nil];

    NSURL *url = [NSURL URLWithString:@" someurl "];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    NSData *requestData = [NSJSONSerialization dataWithJSONObject:dictionary options:kNilOptions error:&error];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody: requestData];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
        if(error || !data)
        {
            NSLog(@"JSON Data not posted!");
            [activity stopAnimating];
            UIAlertView *alertMessage = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Data not saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertMessage show];
        }
        else
        {
            [activity startAnimating];
            NSLog(@"JSON data posted! :)");
            NSError *error = Nil;
            NSJSONSerialization *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
            NSLog(@"Response is %@", jsonObject); //Not getting the response here Message = "An error has occurred."
            [activity stopAnimating];
        }

    }];


推荐答案

请小心:

NSArray *keys = [NSArray arrayWithObjects:@"slno", @"Answer", @"order", @"flage", nil];

但您有:

"flag": "Q" 

flage!= flag。

flage != flag.

这就是为什么使用常数很好!

That's why it is good to use constants!

接下来,关于你的问题:

首先,您必须将JSON转换为NSDictionary。

First you have to transform the JSON into a NSDictionary.

之后:

NSArray * array = [jsonDict valueForKey:@"ProjectInviterFQAnswer"];
for(NSMutableDictionary * dict in array){
     [dict setValue:<put here anything you want> forKey:@""];
}

执行此操作后,从jsonDict创建一个新的JSON,你就是完成。

after you do that, create a new JSON from jsonDict, and you're done.

这篇关于iOS:将以下JSON的字符串数组发布到Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆