http://blog.csdn.net/worldzhy/article/details/49982491
It is often used to convert Nsarray and nsdictionary into JSON format, but iOS does not provide a convenient library of methods. We simulate an example, assuming that we need to convert an array of a custom model (class with only a few attributes) into a JSON string, which is passed to the backend as a parameter to the backend, and the implementation method is given below:
[OBJC]View PlainCopy
- Hspicturemodel Array to JSON
- -(NSString *) Picturearraytojson: (nsarray *) Picarr {
- if (Picarr && picarr. Count > 0) {
- Nsmutablearray *arr = [Nsmutablearray arraywithcapacity:0];
- For (Hspicturemodel *model in Picarr) {
- NSData *jsondata = [Object2Json getjson:model options:nsjsonwritingprettyprinted error: nil];
- nsstring *jsontext = [[NSString alloc] initwithdata:jsondata encoding:nsutf8StringEncoding];
- [arr Addobject:jsontext];
- }
- return [self Objarraytojson:arr];
- }
- return Nil;
- }
- Above this method <span style="FONT-SIZE:18PX;" > The first step is to convert a model to a JSON string and then stitch together the strings in the array to get the final JSON string. We have achieved our purpose. The code used for the object2Json is attached to the bottom of the article.
- </span>
- Convert multiple JSON strings to a JSON string
- -(NSString *) Objarraytojson: (nsarray *) array {
- nsstring *jsonstr = @ "[";
- For (Nsinteger i = 0; i < array. Count; ++i) {
- if (i! = 0) {
- JSONSTR = [Jsonstr stringbyappendingstring:@ ","];
- }
- JSONSTR = [Jsonstr stringbyappendingstring:array[i]];
- }
- JSONSTR = [Jsonstr stringbyappendingstring:@ "]"];
- return jsonstr;
- }
Above, we realize the data from the client to the background when the conversion may be required, and then look at how to send the background JSON form of the string, the client to understand the Nsarray or nsdictionary.
[OBJC]View PlainCopy
- JSON string to array
- -(Nsarray *) Stringtojson: (NSString *) jsonstr {
- if (jsonstr) {
- ID tmp = [nsjsonserialization jsonobjectwithdata:[jsonstr datausingencoding:nsutf8StringEncoding] options:nsjsonreadingallowfragments | nsjsonreadingmutableleaves | Nsjsonreadingmutablecontainers error: nil];
- if (TMP) {
- if ([tmp Iskindofclass:[nsarray class]]) {
- return tmp;
- } Else if ([tmp iskindofclass:[nsstring class]]
- || [TMP Iskindofclass:[nsdictionary class]]) {
- return [Nsarray arraywithobject:tmp];
- } Else {
- return Nil;
- }
- } Else {
- return Nil;
- }
- } Else {
- return Nil;
- }
- }
This standalone approach enables the ability to convert back-end JSON strings to Nsarray or nsdictionary.
Finally, the implementation of the Object2json class is attached, and the implementation is excerpt from the network.
[OBJC]View PlainCopy
- #import "Object2Json.h"
- #import <objc/runtime.h>
- @implementation Object2Json
- + (nsdictionary*) GetObjectData: (id) obj
- {
- nsmutabledictionary *dic = [nsmutabledictionary dictionary];
- unsigned int propscount;
- objc_property_t *props = class_copypropertylist ([obj class], &propscount);
- For (int i = 0;i < Propscount; i++)
- {
- objc_property_t prop = Props[i];
- nsstring *propname = [NSString stringwithutf8string:property_getname (prop)];
- id value = [obj valueforkey:propname];
- if (value = = nil)
- {
- Value = [NSNull null];
- }
- Else
- {
- Value = [self getobjectinternal:value];
- }
- [DiC Setobject:value Forkey:propname];
- }
- return dic;
- }
- + (nsdata*) Getjson: (ID) obj options: (nsjsonwritingoptions) Options Error: (nserror**) Error
- {
- return [nsjsonserialization datawithjsonobject:[self getobjectdata:obj] options:options Error:error];
- }
- + (ID) getobjectinternal: (id) obj
- {
- if ([obj iskindofclass:[nsstring class]]
- || [Obj Iskindofclass:[nsnumber class]]
- || [Obj iskindofclass:[nsnull class]])
- {
- return obj;
- }
- if ([obj Iskindofclass:[nsarray class]])
- {
- nsarray *objarr = obj;
- Nsmutablearray *arr = [Nsmutablearray arraywithcapacity:objarr. Count];
- For (int i = 0;i < Objarr. Count; i++)
- {
- [Arr setobject:[self getobjectinternal:[objarr objectatindex:i]] atindexedsubscript:i];
- }
- return arr;
- }
- if ([obj iskindofclass:[nsdictionary class]])
- {
- nsdictionary *objdic = obj;
- nsmutabledictionary *dic = [nsmutabledictionary dictionarywithcapacity:[objdic Count]];
- For (nsstring *key in Objdic. AllKeys)
- {
- [DiC setobject:[self getobjectinternal:[objdic Objectforkey:key]] Forkey:key];
- }
- return dic;
- }
- return [self getobjectdata:obj];
- }
- @end
IOS converts Nsarray, nsdictionary to JSON format for network transfer