Reputation: 6134
I have to send the following JSON data in request for a web service. So how can I create the following type of JSON using iOS/objective C:
{
"version": "1.0",
"myData": [
{
"name": "",
"value": [
{
"time": "1-JAN-2013 14:30:00 IST",
"values": [
{
"name": "",
"value": ""
}
]
}]
}]
}
I tried :
NSDictionary* version = [NSDictionary dictionaryWithObjectsAndKeys:
[1.0 objectForKey:@"version"], [NSArray objectForKey:@"observations"]
Am I missing something ?
Upvotes: 0
Views: 89
Reputation: 6134
NSMutableArray *output = [[NSMutableArray alloc] init]; NSMutableDictionary *outputObjects = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"temp-value",@"name", @"72.0",@"value",nil]; [output addObject:outputObjects];
NSMutableArray * myData = [[NSMutableArray alloc] init];
NSMutableDictionary *recordObjects = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"23-JUL-2014 10:42:00 IST",@"time",
output,@"value",
nil];
[records addObject:recordObjects];
NSMutableArray *observationValues = [[NSMutableArray alloc] init];
NSMutableDictionary *observationValuesObjects = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"val",@"name",
records,@"record",nil];
[observationValues addObject:observationValuesObjects];
NSDictionary *postData = [NSDictionary dictionaryWithObjectsAndKeys:
@"1.0.1",kPostData,
observationValues,@"observations",
nil]
Upvotes: 0
Reputation: 179
I think it must be like this
NSArray arrObservations=[NSArray arrayWithObject:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"name",arrayValue,@"value", nil]];
NSDictionary* version = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithDouble:1.0],@"version",arrayObservations,@"myData", nil];
Upvotes: 0
Reputation: 13020
Try this i am using below method
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
options:NSJSONWritingPrettyPrinted
error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init] ;
receivedData = [[NSMutableData alloc] initWithLength:0];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection= [[NSURLConnection alloc]initWithRequest:request delegate:self];
[connection start];
Upvotes: 1