従業員オブジェクトを JSON ファイルに変換する次のコードがあります。
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:emp.empName,@"name",emp.empId,@"empid",emp.empAddress,@"address",emp.mobile,@"mobile",nil];
NSData *jsonData=[NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
NSString *str=[[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"data =%@",str);
このコードを使用して、JSON ファイルを取得しています。しかし、このjsonをhttprequestボディとして送信したいのですが、これには次のコードがあります:
NSData *requestBody=[[NSString stringWithFormat:@"%@",str] dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:requestBody];
NSURLConnection *conn=[[NSURLConnection alloc]initWithRequest:request delegate:self];
しかし、サーバー側で解析できない間違ったjsonを取得しています。この問題の解決策を提案してください。JSONデータを直接送信すると、サーバー側で次のjsonが間違っています:
{ '{\n "name" : "vx",\n "mobile" : "8888888",\n "empid" : "96",\n "address" : "addre"\n}': '' }