私はPOJOからJSONに変換するためにJackson側をサーバー側で広範囲に使用していましたが、Objective C/iPhone SDK用の同様のライブラリがあるかどうか、またその逆の場合は不思議でした。目標Cは反射を提供するので、Jacksonと同様のものを作ることが可能でなければならない。iPhone用のJackson相当品ですか?
答えて
Jacksonからインスパイアされたコンベンションオーバレイングパターンを使用して、JSONオブジェクトとObjective-Cオブジェクトを変換するGoldenFleeceを試してみることもできます。
これは私にとって遅すぎると私は自己宣伝に気付くが、私はこのライブラリを書くあなたの努力に感謝する。現在の技術水準は痛いものでした。 –
new iOS 5 APIsは、JSONの読み書きに優れた機能を提供します。これらは基本的にiOS 4で使用できるTouchJSON libraryの再ハッシュです。例のペイロードからPOCOオブジェクトが生成されることはあまりありませんが、NSDictionary
インスタンスのファサードだけのクラスを作成できます上記の図書館が戻るでしょう。例えば
:
@interface PBPhoto : NSObject {
NSDictionary* data_;
}
@property (nonatomic, retain, readonly) NSDictionary *data;
- (NSString*) photoId;
- (NSString*) userId;
- (NSString*) user;
- (NSString*) title;
- (id) initWithData:(NSDictionary*)data;
@end
実装:
のObjective-Cは、反射年の控えめであってもよい提供するが、多くのものは、低レベルによってのみ露出されることを#import "PBPhoto.h"
#define PHOTO_ID @"id"
#define USER_ID @"user_id"
#define USER @"user"
#define TITLE @"title"
@implementation PBPhoto
@synthesize data = data_;
- (id) initWithData:(NSDictionary*)data {
if ((self = [super init])) {
self.data = data;
}
return self;
}
- (NSString*) photoId {
return [super.data objectForKey:PHOTO_ID];
}
- (NSString*) userId {
return [self.data objectForKey:USER_ID];
}
- (NSString*) user {
return [self.data objectForKey:USER];
}
- (NSString*) title {
return [self.data objectForKey:TITLE];
}
- (void) dealloc {
[data_ release];
[super dealloc];
}
@end
すべてのクラスのすべてのプロパティのゲッターを実装する必要がないのは便利です。それが反射が有用な場所です。 –
Cのランタイムとは少し混乱しています。
その後、第三者のために自分のすべてをNSJSONSerialization
にそれをオフに渡す(または他の文字列を構築し、おそらくスマート事が仲介者としてNSDictionary
を作成することで、あなたは任意のオブジェクトを取得し、JSONにそれを有効にすると仮定すると、ライブラリは、デシリアライズできるという負担のため、かなり重量があります)。
したがって、たとえば:
- (NSDictionary *)dictionaryOfPropertiesForObject:(id)object
{
// somewhere to store the results
NSMutableDictionary *result = [NSMutableDictionary dictionary];
// we'll grab properties for this class and every superclass
// other than NSObject
Class classOfObject = [object class];
while(![classOfObject isEqual:[NSObject class]])
{
// ask the runtime to give us a C array of the properties defined
// for this class (which doesn't include those for the superclass)
unsigned int numberOfProperties;
objc_property_t *properties =
class_copyPropertyList(classOfObject, &numberOfProperties);
// go through each property in turn...
for(
int propertyNumber = 0;
propertyNumber < numberOfProperties;
propertyNumber++)
{
// get the name and convert it to an NSString
NSString *nameOfProperty = [NSString stringWithUTF8String:
property_getName(properties[propertyNumber])];
// use key-value coding to get the property value
id propertyValue = [object valueForKey:nameOfProperty];
// add the value to the dictionary —
// we'll want to transmit NULLs, even though an NSDictionary
// can't store nils
[result
setObject:propertyValue ? propertyValue : [NSNull null]
forKey:nameOfProperty];
}
// we took a copy of the property list, so...
free(properties);
// we'll want to consider the superclass too
classOfObject = [classOfObject superclass];
}
// return the dictionary
return result;
}
次にあなたが返される辞書でNSJSONSerialization
に+ dataWithJSONObject:options:error:
を使用することができます。
逆の言い方をすれば、allKeys
とvalueForKey:
を経由してキーと値を辞書から取得して、setValue:forKey:
のキー値コーディングを使用すると思います。
ありがとう、ええこれは私が心に持っていたものですが、誰かが私のために頑張ってくれたことを願っていました。私はあなたの事例を基礎として使って、自分自身で何かを鞭打ちしようと思う。 –
- 1. PushbackReader相当品
- 2. Memcached HDD相当品
- 3. @JsonUnwrappedに相当するJacksonの逆シリアル化は何ですか?
- 4. JSONObjectに相当するJackson 2とは何ですか?
- 5. ハスケルのファブリック相当品?
- 6. C#Message.CreateMessage()Android/Java相当品
- 7. NSData、NSDictionary、NSArray相当品C++
- 8. Watir :: Safari js eval相当品?
- 9. Memcached相当品はC#で書かれていますか?
- 10. Jackson ObjectMapperの代替品
- 11. sound.drv(16ビットサウンドドライバ)のWindows 32ビット相当品がありますか?
- 12. preg_match_all JS相当ですか?
- 13. Android:getElementsByTagName相当ですか?
- 14. BufferedImage.getRGB C#相当ですか?
- 15. JavaFX SwingWorker相当ですか?
- 16. NSTokenFieldコントロールに相当するiPhoneはありますか?
- 17. iPhoneのMPMoviePlayerControllerの代替品ですか?
- 18. iPhoneのデフォルト画像のAndroidに相当する
- 19. 他のアプリを開くためのアンドロイドインテントに相当するiphone
- 20. jQueryのscriptaculous Builder相当ですか?
- 21. git相当の 'hg share'ですか?
- 22. JavascriptのJOptionPane相当ですか?
- 23. DryIoc、Spring.NetのGetObjectsOfType相当ですか?
- 24. IIS6用のSF_NOTIFY_READ_RAW_DATAに相当する
- 25. Javaで__LINE__相当ですか?
- 26. 相当
- 27. IsFinite()相当の
- 28. 相当のスコープ?
- 29. iphone用キーボードの代替品は何ですか?
- 30. Rubyは "相当"です
ここに言語の壁がある可能性があるので、明確にしてください。宣言されたプロパティに基づいてオブジェクトをJSONに自動的にシリアル化するコードが必要ですか? – Tommy
はい、私たちのサーバーがJSONエンコードされたレスポンスを送信するので、もう一方の方法も便利です。 –