2

Json を使用して objc オブジェクトをシリアル化し、サーバーに送信しようとしています。

その同じサーバーは、このオブジェクト タイプの GET で次を送信します。

 {
   "TypeProperties":[
      {"Key":"prop 0","Value":"blah 0"},
      {"Key":"prop 1","Value":"blah 1"},
      {"Key":"prop 2","Value":"blah 2"},
      {"Key":"prop 3","Value":"blah 3"}
     ],
   "ImageURls":[
      {"Key":"url 0","Value":"blah 0"},
      {"Key":"url 1","Value":"blah 1"},
      {"Key":"url 2","Value":"blah 2"},
      {"Key":"url 3","Value":"blah 3"}
     ]
}

SBJsonWriter は、objc で作成した一致するオブジェクト/型に対して次を生成します。

{
  "TypeProperties": {
    "key 2": "object 2",
    "key 1": "object 1",
    "key 4": "object 4",
    "key 0": "object 0",
    "key 3": "object 3"
  },
  "ImageUrls": {
    "key 0": "url 0",
    "key 1": "url 1",
    "key 2": "url 2"
  }
}

これは私がSBJsonWriterを使用している方法です:

SBJsonWriter *writer = [[SBJsonWriter alloc] init];
writer.humanReadable = YES;
NSString* json = [writer stringWithObject:itemToAdd];

シリアル化されるクラスでの proxyForJson の実装を次に示します (SBJsonWriter で必要)。

- (NSDictionary*) proxyForJson
{
      return [NSMutableDictionary dictionaryWithObjectsAndKeys:
                self.typeProperties, @"TypeProperties",
                self.imageUrls, @"ImageUrls",
                nil];
}

シリアル化されるクラスには、typeProperties と imageUrls の 2 つのプロパティのみが含まれます (どちらも NSMutableDictionary です)。

ここで、問題: サーバーは (当然のことながら) POST を実行したときに SBJsonWriter によって生成された Json を解析しません。そして質問: サーバーによって提供されたものと一致する Json をどのように生成するのですか (一致する Json がアップロード時に正しく解析されると仮定します)。

助けてくれてありがとう。

4

2 に答える 2

5

以下は、SBJsonWriter の使用方法を示す簡単なコードです。

#import <Foundation/Foundation.h>

#import "SBJsonWriter.h"


int main(int argc, const char * argv[])
{

    @autoreleasepool {

        NSDictionary* aNestedObject = [NSDictionary dictionaryWithObjectsAndKeys:
                                              @"nestedStringValue", @"aStringInNestedObject",
                                              [NSNumber numberWithInt:1], @"aNumberInNestedObject",
                                         nil];

        NSArray * aJSonArray = [[NSArray alloc] initWithObjects: @"arrayItem1", @"arrayItem2", @"arrayItem3", nil];

        NSDictionary * jsonTestDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                             @"stringValue", @"aString",
                                             [NSNumber numberWithInt:1], @"aNumber",
                                             [NSNumber numberWithFloat:1.2345f], @"aFloat",
                                             [[NSDate date] description], @"aDate",
                                             aNestedObject, @"nestedObject",
                                             aJSonArray, @"aJSonArray",
                                             nil];

        // create JSON output from dictionary

        NSError *error = nil;
        NSString * jsonTest = [[[SBJsonWriter alloc] init] stringWithObject:jsonTestDictionary error:&error];

        if ( ! jsonTest ) {
            NSLog(@"Error: %@", error);
        }else{
            NSLog(@"%@", jsonTest);
        } 
    }
    return 0;
}

出力

{
    "aDate":"2012-09-12 07:39:00 +0000",
    "aFloat":1.2345000505447388,
    "nestedObject":{
        "aStringInNestedObject":"nestedStringValue",
        "aNumberInNestedObject":1
     },
    "aJSonList":["arrayItem1","arrayItem2","arrayItem3"],
    "aString":"stringValue",
    "aNumber":1
}

ノート:

  1. 「エラー」を使用すると、[[NSDate date] description] の代わりに [NSDate date] を記述すると、「__NSTaggedDate では JSON シリアル化がサポートされていません」というエラーが発生することがわかりました。
  2. float の丸めエラーに注意してください... 1.2345 が 1.2345000505447388 になりました
于 2012-09-12T07:54:34.360 に答える
2

JSONでは{ }、オブジェクト(キーと値のペア)を[ ]表し、配列を表します。提供したサンプルから判断すると、サーバーが期待するものは次のとおりです。

トップオブジェクト:2つのキーを持つ辞書:TypePropertiesImageUrls

TypePropertiesImageUrls:それぞれは、2つのキーを持つ1つ以上のオブジェクトを含む配列です:KeyValue。各キーにはそれぞれの値が必要です。

サーバーが期待するものに準拠するには、次のような構造が必要です(これは単なる例であり、ここに直接記述されていますが、正しい方向を示している必要があります)。

NSDictionary *object = [NSDictionary dictionaryWithObjectsAndKeys:
                        @"prop 0", @"Key",
                        @"blah 0", @"Value",
                        nil];

NSArray *typeProperties = [NSArray arrayWithObjects:
                           object, // Add as many similar objects as you want
                           nil];

NSArray *imageUrls = [NSArray arrayWithObjects:
                      object, // Add as many similar objects as you want
                      nil];

次に、proxyForJsonメソッドで次を使用できます。

- (NSDictionary*) proxyForJson
{
      return [NSDictionary dictionaryWithObjectsAndKeys:
              typeProperties, @"TypeProperties",
              imageUrls, @"ImageUrls",
              nil];
}
于 2012-08-02T02:42:20.323 に答える