在 iOS 中将字典的 NSArray 转换为 JSON 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12299965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting an NSArray of Dictionaries to JSON array in iOS
提问by iOS Developer
I need to send an NSArrayto the server in the JSONarray format. How can I convert it to JSON. This is a sample of my NSArraythat I have to pass.
我需要以数组格式NSArray向服务器发送一个JSON。我怎样才能将它转换为JSON. 这是NSArray我必须通过的样本。
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.
有人有解决方案吗?提前致谢。
回答by prashant
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);
回答by Kunal Gupta
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
要将 NSArray 或 NSMutableArray 转换为 jsonString,您可以先将其转换为 NSData,然后进一步将其转换为 NSString。使用此代码
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.
它帮助了我,希望它也能帮助你。祝一切顺利。
回答by Shelm
I would recommend the SBJson-Framework.
我会推荐SBJson-Framework。
Converting an NSMutableArrayis as simple as NSString *jsonString = [yourArray JSONRepresentation];
转换一个NSMutableArray就像这样简单NSString *jsonString = [yourArray JSONRepresentation];
Edit: Hyman 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.
编辑:Hyman Farnandish 是对的,您必须先将其转换为 NSDictionary,然后才能将其转换为 Json。在我的示例中,NSMutableArray 必须包含字典。Array 只需要在字符串的开头和结尾创建方括号。
回答by Hyman Farnandish
First You must change you structure into NSDictionaryclass and NSArraycontaining NSDictionaryobjects, then try JSONKit in iOS 5 serialization works better than standard NSJSONSerialization.
首先,您必须将结构更改为NSDictionary类并NSArray包含NSDictionary对象,然后尝试在 iOS 5 序列化中使用 JSONKit 比标准更好NSJSONSerialization。
回答by jnic
回答by E. Lüders
You can use the build in JSON functionsof iOS or use an external lib e.g. JSONKitto convert your data to JSON
您可以使用iOS的内置 JSON 函数或使用外部库(例如JSONKit)将数据转换为 JSON
回答by Durga Vundavalli
回答by Dan Morrow
Is the service you are calling a RESTful service?
您正在调用的服务是 RESTful 服务吗?
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.
如果是这样,我强烈建议使用 RestKit。它执行对象序列化/反序列化。它还处理所有网络基础。非常珍贵,而且保养得很好。

