Converting an NSArray of Dictionaries to JSON array in iOS

后端 未结 8 1159
逝去的感伤
逝去的感伤 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: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.

提交回复
热议问题