I'm trying to serialize an objc object with Json to send it to a server.
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 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"
}
}
This is how I'm using SBJsonWriter:
SBJsonWriter *writer = [[SBJsonWriter alloc] init];
writer.humanReadable = YES;
NSString* json = [writer stringWithObject:itemToAdd];
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];
}
The class being serialized contains only the two properties: typeProperties and imageUrls (both are NSMutableDictionary).
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).
Thanks in advance for any help.
In JSON,
{ }
represents an object (key/value pairs) and[ ]
represents an array. Judging by the sample you provided, here's what your server expects:Top Object: dictionary with two keys:
TypeProperties
andImageUrls
.TypeProperties and ImageUrls: each is an array containing one or more objects with two keys:
Key
andValue
. 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):
Then, in your
proxyForJson
method, you can use:Here is a simple code demonstrating how to use SBJsonWriter:
outputs
Notes: