Converting an NSArray of Dictionaries to JSON array in iOS

空扰寡人 提交于 2019-11-28 17:37:40
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);

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.

Shelm

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.

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

Jack Farnandish

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.

#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.

Durga Vundavalli

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!