一个简单的 JSON 解析库,旨在遵循 JSON 规范并提供一些类型安全性。
NSData *profileData = [@"{ \"name\": \"Mickey Mouse\", \"details\": { \"age\": 102, \"species\": \"mouse\" }, \"animated\": true, \"friends\": [\"Minnie\", \"Goofy\", \"Donald\"] }" dataUsingEncoding:NSUTF8StringEncoding];
JSON *profileJSON = [JSON fromData:data];
NSString *mickeysName = profileJSON["name"].asString;
BOOL isAnimated = profileJSON["animated"].isTrue;
NSInteger mickeysAge = profileJSON["details"]["age"].asNumber.integerValue;
NSString *mickeysSpecies = profileJSON["details"]["species"].asString;
NSString *mickeysFirstFriendsName = profileJSON["friends"][0].asString;
JSON *minniesNameJSON = profileJSON["friends"][0]["name"]; // => JSON
NSString *minniesName = minniesNameJSON.asString; // => nil
NSError *minniesNameError = minniesNameJSON.asError; // => NSError(domain: JSON, code: 422)
NSString *mickeysLocation = profileJSON["location"].asError; // => NSError(domain: JSON, code: 404)
profileJSON.isObject; // => YES
profileJSON["name"].isString // => YES
profileJSON["animated"].isString // => NO
profileJSON["animated"].isTrue // => YES
NSData *profileData = profileJSON.asJSON; // => { "name": "Mickey Mouse", "details": { "age": 102, "species": "mouse" }, "animated": true, "friends": ["Minnie", "Goofy", "Donald"] } as NSData
NSData *detailsData = profileJSON["details"].asJSON; // => { "age": 102, "species": "mouse" } as NSData
JSON *myJSON = [JSON emptyObject];
// TODO: Mutator methods
标准 MIT 许可协议。请享受。