ios中解析json物件基類

2021-09-08 09:47:52 字數 1603 閱讀 7289

這個是對上面一篇寫的乙個解析json物件的基類

@inte***ce baseobjectfromjson : nsobject  

+ (id)objectwithdict:(nsdictionary*)dictwithclassname:(nsstring*)classname;  

+ (nsarray*)objectswitharray:(nsarray*)arraywithclassname:(nsstring*)classname;  

- (id)initwithdict:(nsdictionary*)dict;  

- (nsstring*)propertyclassname:(nsstring*)propertyname;  

@end

////  baseobjectfromjson.m

//  xxoo

////  created by tommy on 13-11-5.

//#import "baseobjectfromjson.h"

#import "objcutils.h"

@implementation baseobjectfromjson  

+ (id)objectwithdict:(nsdictionary*)dictwithclassname:(nsstring*)classnameelse  

return object;  

}  + (nsarray*)objectswitharray:(nsarray*)arraywithclassname:(nsstring*)classname  

}else  

return objarray;  

}  - (nsstring*) description  

return desc ;  

}  - (id)initwithdict:(nsdictionary*)dictelse

if([dict[key]iskindofclass:[nsdictionaryclass]])else  

}@catch (n***ception *exception)   

}  return

self;  

}  - (nsstring*)propertyclassname:(nsstring*)propertyname  

@end

nsstring* jsonstring = request.responsestring;  

nsdata* jsondata = [jsonstringdatausingencoding:nsutf8stringencoding];  

nsdictionary *jsonobj = [jsondataobjectfromjsondata];  

result* result = [searchresultobjectwithdict:jsonobjwithclassname:@"result"];  

如果乙個需要對乙個json物件的解析中不存在陣列之類的,就直接繼承就行,什麼都不用寫。如果有陣列的話,就需要過載

- (nsstring*)propertyclassname:(nsstring*)propertyname這個方法就行。這樣就可以找到正確的類進行解析了。  

ios中解析json物件基類

這個是對上面一篇寫的乙個解析json物件的基類 inte ce baseobjectfromjson nsobject id objectwithdict nsdictionary dict withclassname nsstring classname nsarray objectswithar...

IOS中JSON資料解析

官方為我們提供的解析json資料的類是 nsjsonserialization,首先我們先來看下這個類的幾個方法 bool isvalidjsonobject id obj 判斷乙個資料物件是否可以轉化為json資料 nsdata datawithjsonobject id obj options ...

iOS中JSON資料解析

使用方法 nsdictionary testdic nsdictionary alloc initwithobjectsandkeys 0 result 1020 bizcode success data nil nslog testdic testdic nserror error nsdata ...