Converting an NSArray of Dictionaries to JSON array in iOS

后端 未结 8 1095
逝去的感伤
逝去的感伤 2020-12-13 08:09

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

相关标签:
8条回答
  • 2020-12-13 08:44

    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.

    0 讨论(0)
  • 2020-12-13 08:46

    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.

    0 讨论(0)
提交回复
热议问题