It is much easier just to use a JSON serializer.
NSDictionary *JSON = @{ @"drugname" : drugname, @"id" : ID };
NSData *myPostData = [NSJSONSerialization dataWithJSONObject:JSON options:0 error:NULL];
Domanda
I have drugnames like drugnameexmp "20 tbl"
The ' " ' charachter makes trouble for creating a NSString that it is for JSON.
{\"drugname\":\"%@\",drugname}
How can I change '"' charachter for it?The below code is what I tried.
[drug setValue:[[drug objectForKey:@"drug_name"] stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""] forKey:@"drug_name"];
Thank you.
Edited:
It acts like this:
{
"drugname":"drugnameexmp "20 tbl",
"id":123
}
"drugnameexmp "20 tbl" looks like this: "drugnameexmp "
+ 20 tbl"
' " ' parses the NSString. I want " drugnameexmp '20 tbl' "
.
NSString *s=[NSString stringWithFormat:@"{\"drugname\":\"%@\",\"id\":%d,drugname,id} "];
NSData *myPostData = [s dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *myMutablePostData = [[NSMutableData alloc]initWithData:myPostData];
[request setPostBody:myMutablePostData];
[request setRequestMethod:@"POST"];
[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request setDelegate:self];
[request startSynchronous];
Soluzione
It is much easier just to use a JSON serializer.
NSDictionary *JSON = @{ @"drugname" : drugname, @"id" : ID };
NSData *myPostData = [NSJSONSerialization dataWithJSONObject:JSON options:0 error:NULL];