私は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. @JsonUnwrappedに相当するJacksonの逆シリアル化は何ですか?
- 3. Memcached HDD相当品
- 4. Sklearn - predict_proba相当品
- 5. Swift4 initWithArray相当品
- 6. JSONObjectに相当するJackson 2とは何ですか?
- 7. コードギターのジェム相当品
- 8. ハスケルのファブリック相当品?
- 9. C#Message.CreateMessage()Android/Java相当品
- 10. Watir :: Safari js eval相当品?
- 11. NSData、NSDictionary、NSArray相当品C++
- 12. TensorFlow:tf.add_nの製品に相当する
- 13. メガ2560のアナログピン0-15のデジタル相当品は何ですか?
- 14. oracle10gのselect top相当品は何ですか
- 15. Memcached相当品はC#で書かれていますか?
- 16. MongoDB + node.js - SQL相当品を選択
- 17. sound.drv(16ビットサウンドドライバ)のWindows 32ビット相当品がありますか?
- 18. Jackson ObjectMapperの代替品
- 19. rustc/cargoには-march = native相当品がありますか?
- 20. iText7にPdfSmartCopy相当品がありますか
- 21. NSTokenFieldコントロールに相当するiPhoneはありますか?
- 22. preg_match_all JS相当ですか?
- 23. Bazel cuda_add_executable相当ですか?
- 24. Android:getElementsByTagName相当ですか?
- 25. Tarantool - AUTOINCREMENT相当ですか?
- 26. BufferedImage.getRGB C#相当ですか?
- 27. NSView .transform相当ですか?
- 28. JavaFX SwingWorker相当ですか?
- 29. iPhoneのMPMoviePlayerControllerの代替品ですか?
- 30. iPhoneのデフォルト画像のAndroidに相当する
ここに言語の壁がある可能性があるので、明確にしてください。宣言されたプロパティに基づいてオブジェクトをJSONに自動的にシリアル化するコードが必要ですか? – Tommy
はい、私たちのサーバーがJSONエンコードされたレスポンスを送信するので、もう一方の方法も便利です。 –