私はjsonstringを送信するためにjson Frameworkを使用しています。私のJSONのポスト文字列は、私ははjsonにpostStringを送信できません
NSString *postString = [NSString stringWithFormat:@"[\"firstName\":\"barani\",\"lastName\":\"Tharn\",\"contactSource\":\"Leter\",\"permanentAddress\":{},\"deliveryAddress\":{},\"preferredContactTimes\":[]]",fName.text,lName.text,contactSource.text,address,address,add];
NSURL *purchaseURL = [NSURL URLWithString:@"http://localhost/Cali/addContact"];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:purchaseURL];
NSData *requestData = [NSData dataWithBytes:[postString UTF8String] length:[postString length]];
[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];//[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"post String :%@",[[NSString alloc] initWithData:requestData encoding:NSASCIIStringEncoding]);
NSURLConnection *purchaseConn =[[NSURLConnection alloc]
initWithRequest:request
delegate:self];
if (purchaseConn) {
NSLog(@"connection!!!");
webData = [[NSMutableData data] retain];
}
と関連して......
{
"firstName": "string1",
"lastName": "string2",
"contactSource": "Leter",
"permanentAddress": {},
"deliveryAddress": {},
"preferredContactTimes": []
}
コーディングされた仕上がりロード
ここNSString *jsonString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
NSLog(@"hai:%@",[[NSString alloc] initWithData:webData encoding:NSASCIIStringEncoding]);
NSDictionary *jsonValue = [jsonString JSONString];
NSLog(@"output:%@",jsonValue);
permanentAddress
ともdeliveryAddress
サブJSON文字列をしましたadn preferredContactTimesは配列
私はjson失敗したerro r
JSONValueに失敗しました。エラートレースは次のとおりです。( "エラードメイン= org.brautaset.JSON.ErrorDomainコード= 11 \" 文字列\の予期しない終了 "のUserInfo = 0x4c8d820 {文字列のNSLocalizedDescription =予期しない終了}" )
お願いします私の解決策
jsonの形式は正しいです。選択したjsonフレームワークを正しく使用するようにしてください。また、本当にサポートが必要な場合は、さらに詳しい情報を提供してください。 – makaron