在我的iPhone应用程序中,我有一个自定义对象列表.我需要从它们创建一个json字符串.我如何用SBJSON或iPhone sdk实现这个?
NSArray* eventsForUpload = [app.dataService.coreDataHelper fetchInstancesOf:@"Event" where:@"isForUpload" is:[NSNumber numberWithBool:YES]];
SBJsonWriter *writer = [[SBJsonWriter alloc] init];
NSString *actionLinksStr = [writer stringWithObject:eventsForUpload];
Run Code Online (Sandbox Code Playgroud)
我得到空的结果.
我正在尝试使用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"}
]
}
Run Code Online (Sandbox Code Playgroud)
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"
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我使用SBJsonWriter的方式:
SBJsonWriter *writer = [[SBJsonWriter alloc] init]; …Run Code Online (Sandbox Code Playgroud)