Note that there are some explanatory texts on larger screens.

plurals
  1. POSending string as JSON object
    primarykey
    data
    text
    <p>I have an assignment that requires me to put a string as a json object and before sending the object i have to put this json object into a http header. This is my code:</p> <pre><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:&amp;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]; </code></pre> <p>My problem is that i can't use the jsonDictionary with objects and send it, cuz the string format that the service has to receive is this:</p> <p>"{"nid":"","vocab":"", "inturl":"testoverview", "mail":"", "md5pw":""}"</p> <p>The dictionary would insert = in the string and that would give me no response from the service.</p> <p>I want to send a string (json in the code) as dataWithJSONObject:jso, likes this:</p> <pre><code>NSData *jsonData = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:&amp;error]; </code></pre> <p>but i get an error satating this: </p> <p><strong>Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*</strong> +<strong>[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write'</strong></p> <p>can anybody help me with this ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload