16

I need to send an NSArray to the server in the JSON array format. How can I convert it to JSON. This is a sample of my NSArray that I have to pass.

array([0] => array('latitude'=>'10.010490', 
                  'longitude'=>'76.360779', 
                   'altitude'=>'30.833334', 
                  'timestamp'=>'11:17:23', 
                      'speed'=>'0.00', 
                   'distance'=>'0.00');

[1] => array('latitude'=>'10.010688', 
            'longitude'=>'76.361378', 
             'altitude'=>'28.546305', 
            'timestamp'=>'11:19:26', 
                'speed'=>'1.614', 
             'distance'=>'198.525711')
 )`

and the required format is like this

[
  { "latitude":"10.010490",
   "longitude":"76.360779",
    "altitude":"30.833334",
   "timestamp":"11:17:23", 
       "speed":"0.00",
    "distance":"0.00"
  },    
  {
   "latitude":"10.010688",
  "longitude":"76.361378",
   "altitude":"28.546305",
  "timestamp":"11:19:26",
      "speed":"1.614",
   "distance":"198.525711" 
  }
]

Any one have solution? Thanks in advance.

8 Answers 8

52
NSDictionary *firstJsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"10.010490", @"latitude",
                            @"76.360779", @"longitude",
                            @"30.833334", @"altitude",
                            @"11:17:23", @"timestamp",
                            @"0.00", @"speed",
                            @"0.00", @"distance",
                            nil];

  NSDictionary *secondJsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"10.010490", @"latitude",
                            @"76.360779", @"longitude",
                            @"30.833334", @"altitude",
                            @"11:17:23", @"timestamp",
                            @"0.00", @"speed",
                            @"0.00", @"distance",
                            nil];

NSMutableArray * arr = [[NSMutableArray alloc] init];

[arr addObject:firstJsonDictionary];
[arr addObject:secondJsonDictionary];

NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
NSLog(@"jsonData as string:\n%@", jsonString);
Sign up to request clarification or add additional context in comments.

2 Comments

It works for me but I want response in array format not in string format.
inline 'code [ { "latitude":"10.010490", "longitude":"76.360779", "altitude":"30.833334", "timestamp":"11:17:23", "speed":"0.00", "distance":"0.00" }, { "latitude":"10.010688", "longitude":"76.361378", "altitude":"28.546305", "timestamp":"11:19:26", "speed":"1.614", "distance":"198.525711" } ] ' you will get it in Array format only
5

The simplest and best approach !!!

To convert NSArray or NSMutableArray into jsonString you can first convert it into NSData and then further convert that into a NSString. Use this code

NSData* data = [ NSJSONSerialization dataWithJSONObject:yourArray options:NSJSONWritingPrettyPrinted error:nil ];
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

It helped me and hope it helps you as well. All the best.

1 Comment

This is the correct answer to the question title. Thanks. The accepted answer shows an array of dictionaries
4

I would recommend the SBJson-Framework.

Converting an NSMutableArray is as simple as NSString *jsonString = [yourArray JSONRepresentation];

Edit: Jack Farnandish is right u have to transform it into a NSDictionary before you can convert it to Json. In my example the NSMutableArray has to contain the Dictionary. The Array is only needed to create the square brackets at the beginning and the end of the string.

Comments

2

You can use the build in JSON functions of iOS or use an external lib e.g. JSONKit to convert your data to JSON

Comments

2

First You must change you structure into NSDictionary class and NSArray containing NSDictionary objects, then try JSONKit in iOS 5 serialization works better than standard NSJSONSerialization.

Comments

2
#import <JSONKit/JSON.h>

NSArray *array = // Your array here.
NSString *json = [array JSONString];

NSLog(@"%@", json);

JSONKit performs significantly better than SBJson and others in my own and the author's benchmarks.

Comments

2

Check this tutorial, JSON in iOS 5.0 was clearly explained (serailization, deserailization).

Comments

1

Is the service you are calling a RESTful service?

If so, I'd strongly recommend using RestKit. It does object serialization/deserialization. It also handles all the networking underpinnings. Extremely valuable, and well maintained.

1 Comment

Using framework for such a simple task is not really a good idea. Update 2016 RestKit is not a maintained framework any more.

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.