我使用NSDictionary创建了一个json。这是我的代码。
NSArray *propertyNames1 = [NSArray arrayWithObjects:@"Name", @"Lat", @"Lng", nil];
NSArray *propertyValues1 = [NSArray arrayWithObjects:@"testName1", @"32.345453", @"23.5456346", nil];
NSDictionary *properties1 = [NSDictionary dictionaryWithObjects:propertyValues1 forKeys:propertyNames1];
我想将它发送到网址。因为我使用了这段代码。
NSString *urlString=[NSString stringWithFormat:@"http:example.com];
NSMutableURLRequest *mrequest = [[NSMutableURLRequest alloc] init];
[mrequest setURL:[NSURL URLWithString:urlString]];
[mrequest setHTTPMethod:@"PUT"];
[mrequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSString *post =[NSString stringWithFormat:@"%@",properties1];
NSData *body = [NSData dataWithBytes: [post1 UTF8String] length:[post1 length]];
[mrequest setHTTPBody:body];
NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:mrequest delegate:self];
if(connection)
{
webdata = [[NSMutableData alloc] init];
}
但是它会以
的形式发布到网址中{ “{\ n Lat”:“\”32.345453 \“; \ n Lng = \”23.5456346 \“; \ n Name = testName1; \ n}” }
我需要输出
{
{
"Lat": "32.345453"
"Lng": "23.5456346"
"Name": "testName1"
}
}
请帮帮我......
答案 0 :(得分:2)
use the following
jsonDictionary = [[NSMutableDictionary alloc] init];
[jsonDictionary setObject:@value1" forKey:@"Lat"];
[jsonDictionary setObject:@"value2" forKey:@"Lng"];
[jsonDictionary setObject:@"value3" forKey:@"Name"];
//Set the URL YOU WILL PROVIDE
NSURL *url = [NSURL URLWithString:@"http://....."];
NSData *jsonData=[NSJSONSerialization dataWithJSONObject:jsonDictionary
options:NSJSONWritingPrettyPrinted
error:nil];
ASIFormDataRequest *request= [[ASIFormDataRequest alloc] initWithURL:url :@"POST"];
[request setData:jsonData forKey:nil];
[request setDelegate:self];
[request startAsynchronous];
祝你好运:)
答案 1 :(得分:1)
//构建一个info对象并转换为json
NSDictionary *properties1 = [NSDictionary dictionaryWithObjects:propertyValues1 forKeys:propertyNames1];
//convert object to data
NSData* jsonData = [NSJSONSerialization dataWithJSONObject: properties1 options:kNilOptions error:&error];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://example.com"]];];
[request setHTTPMethod:@"PUT"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:jsonData];
// print json:
NSLog(@"JSON summary: %@", [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding]);
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(connection)
{
webdata = [[NSMutableData alloc] init];
}
答案 2 :(得分:0)
要将字典转换为JSON,您不应使用以下行:
NSString *post =[NSString stringWithFormat:@"%@",properties1];
而不是你应该使用下面的行:
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:properties1
options:0
error:&error];
答案 3 :(得分:0)
使用SBJSON https://github.com/dekkers/sbjson
import JSON.h
NSString *post =[NSString stringWithFormat:@"%@",[properties1 JSONRepresentation]];