0

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 ?

3
  • 1
    I tend to use NSMutableDictionary *jsonDict to create the json dictionary so that I can use [jsonDict setValue:"1.0" forKey:@"version"]; or [jsonDict setValue:myDataArray forKey:@"myData"]; Your above "dictionaryWithObjectsAndKeys" method expect your list to be in order of value, key, value, key. So if you want to use that method, you'd put [NSDictionary dictionaryWithObjectsAndKeys: @"1.0", @"version", myDataArray, @"myData", nil]; Commented Jul 23, 2014 at 4:31
  • seems to be unclear, where is the code for NSJSONSerialization of NSJSon object. And what you're trying to achieve for version. Commented Jul 23, 2014 at 4:36
  • The above statement to be sent in my request body. I am aware that, I should create - [NSJSONSerialization dataWithJSONObject:... But, my question is how to form such request body? Commented Jul 23, 2014 at 4:43

3 Answers 3

1

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];
Sign up to request clarification or add additional context in comments.

3 Comments

i have a doubt on - NSJSONWritingPrettyPrinted? Did you refer to the json typed in above or the NSDictionary thing?
I have sent the NSDictionary using this. No its fine.
This helps in understanding how to form JSON data form NSdictionary.
0
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];

1 Comment

This is correct, but we should add a mutable array.
0

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.