Sending a string as a JSON object

I have a job that requires me to put the string as a json object, and before sending the object, I have to put this json object in the http header. This is my code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *nid = @"";
    NSString *vocab = @"";
    NSString *inturl = @"testoverview";
    NSString *mail = @"chh@fbr.dk";
    NSString *md5pw = @"4d57e7ef1b7c3f431aca424764e9d786";

    NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                    nid, @"nid", 
                                    vocab, @"vocab",
                                    inturl, @"inturl",
                                    mail, @"mail",
                                    md5pw, @"md5pw",nil];


    NSString *json = @"{nid:"",vocab:"",inturl:testoverview, mail:chh@fbr.dk, md5pw:4d57e7ef1b7c3f431aca424764e9d786}";


    NSError *error;



    NSString *url = @"http://udv.taenk.dk/chh/drupal-taenk/services/mobile";


    NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:&error];


    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLCacheStorageAllowed timeoutInterval:30.0];

     NSURLConnection *connction = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    FSRemoteExecutor *remote = [[FSRemoteExecutor alloc] init];
    [remote execute:url andHandler:[FSProductTestHandler alloc] JSONString:jsonString JSONData:jsonData Connection:connction];
    [remote connection:connction didReceiveData:jsonData];
    [remote connectionFinishedLoading:connction];

My problem is that I cannot use jsonDictionary with objects and send it, because the string format the service should receive is this:

"{" nid ":" "," vocab ":" "," inturl ":" testoverview "," mail ":" "," md5pw ":" "}"

The dictionary will insert = into the string, and this will not give me a response from the service.

I want to send a string (json in code) as dataWithJSONObject: jso, like this:

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:&error];

but I get an error:

- "NSInvalidArgumentException", : "* + [NSJSONSerialization dataWithJSONObject: options: error:]: JSON write '

- ?

+3
2

use NSJSONSerialization ? JSON (NSString *json = @"{nid:"",vocab:"",inturl:testoverview, mail:chh@fbr.dk, md5pw:4d57e7ef1b7c3f431aca424764e9d786}";). NSJSONSerialization.

NSData *json (NSString), .

NSData :

NSData *jsonPayload = [json dataUsingEncoding:NSUTF8StringEncoding];
+12

-, json , . "" , . :

NSString *json = @"{nid:"",vocab:"",inturl:testoverview, mail:chh@fbr.dk, md5pw:4d57e7ef1b7c3f431aca424764e9d786}";

:

NSString *json = @"{nid:"
                 ",vocab:"
                 ",inturl:testoverview, mail:chh@fbr.dk, md5pw:4d57e7ef1b7c3f431aca424764e9d786}";

, . . , :

NSString *json = @"{nid:,vocab:,inturl:testoverview, mail:chh@fbr.dk, md5pw:4d57e7ef1b7c3f431aca424764e9d786}";

.

, , :

NSString *json = @"{nid:\"\",vocab:\"\",inturl:testoverview, mail:chh@fbr.dk, md5pw:4d57e7ef1b7c3f431aca424764e9d786}";

:

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:&error];

, "=".


Update:

( ):

NSString *nid = @"";
NSString *vocab = @"";
NSString *inturl = @"testoverview";
NSString *mail = @"chh@fbr.dk";
NSString *md5pw = @"4d57e7ef1b7c3f431aca424764e9d786";

NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                nid, @"nid", 
                                vocab, @"vocab",
                                inturl, @"inturl",
                                mail, @"mail",
                                md5pw, @"md5pw",nil];

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:&error];
NSString *resultAsString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"jsonData as string:\n%@", resultAsString);

:

jsonData as string:
{
  "md5pw" : "4d57e7ef1b7c3f431aca424764e9d786",
  "nid" : "",
  "inturl" : "testoverview",
  "mail" : "chh@fbr.dk",
  "vocab" : ""
}

0 NSJSONWritingPrettyPrinted, :

jsonData as string:
{"md5pw":"4d57e7ef1b7c3f431aca424764e9d786","nid":"","inturl":"testoverview","mail":"chh@fbr.dk","vocab":""}
+4

All Articles