SBJsonWriter嵌套NSDictionary
我正在尝试使用Json序列化objc对象,以将其发送到服务器.
I'm trying to serialize an objc object with Json to send it to a server.
同一服务器为此对象类型在GET上发送以下内容:
That same server sends the following on GET for this object type:
{
"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中创建的匹配对象/类型生成以下内容:
SBJsonWriter produces the following for the matching object/type that I've created in 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的方式:
This is how I'm using SBJsonWriter:
SBJsonWriter *writer = [[SBJsonWriter alloc] init];
writer.humanReadable = YES;
NSString* json = [writer stringWithObject:itemToAdd];
这是我正在序列化的类中的proxyForJson的实现(SBJsonWriter要求):
Here's my implementation of proxyForJson in the class being serialized (required by SBJsonWriter):
- (NSDictionary*) proxyForJson
{
return [NSMutableDictionary dictionaryWithObjectsAndKeys:
self.typeProperties, @"TypeProperties",
self.imageUrls, @"ImageUrls",
nil];
}
要序列化的类仅包含两个属性:typeProperties和imageUrls(均为NSMutableDictionary).
The class being serialized contains only the two properties: typeProperties and imageUrls (both are NSMutableDictionary).
现在,问题是:执行POST时,服务器(不奇怪)不会解析SBJsonWriter产生的Json.问题是:如何生成与服务器提供的Json相匹配的Json(假设匹配的Json在上载时会被正确解析).
Now, the problem: the server (not surprisingly) does not parse the Json produced by SBJsonWriter when I perform a POST. And the question: how do I produce Json that matches that which is provided by the server (assuming that matching Json would be parsed correctly on upload).
在此先感谢您的帮助.
在JSON中,{ }
表示一个对象(键/值对),而[ ]
表示一个数组.从您提供的示例来看,这是服务器的期望值:
In JSON, { }
represents an object (key/value pairs) and [ ]
represents an array. Judging by the sample you provided, here's what your server expects:
主要对象:带有两个键的字典:TypeProperties
和ImageUrls
.
Top Object: dictionary with two keys: TypeProperties
and ImageUrls
.
TypeProperties 和 ImageUrls :每个数组都是一个包含一个或多个对象的数组,这些对象带有两个键:Key
和Value
.每个键应具有各自的值.
TypeProperties and ImageUrls: each is an array containing one or more objects with two keys: Key
and Value
. Each key should have its respective value.
要符合服务器的期望,您需要一个与此类似的结构(请注意,这只是一个简单的示例,直接在此处编写,但应指出正确的方向):
To comply with what your server expects, you need a structure similar to this (note this is just a bare example, written directly here, but should point you in the right direction):
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
方法中,您可以使用:
Then, in your proxyForJson
method, you can use:
- (NSDictionary*) proxyForJson
{
return [NSDictionary dictionaryWithObjectsAndKeys:
typeProperties, @"TypeProperties",
imageUrls, @"ImageUrls",
nil];
}