简体   繁体   中英

Writing JSON using SBJSON

I have recently started parsing JSON documents using SBJSON Parser, and I am able to read JSON documents just fine. However I am unable to figure out how I am meant to write JSON using this library. Under the documentation

http://stig.github.com/json-framework/api/3.0/interfaceSBJsonStreamWriter.html

There is a class for writing JSON, but I cannot figure out how to use it. There are no tutorials in his documentation on how to use it, and I can't find any tutorials online about using it.

As an example, I tried doing something like this

SBJsonStreamWriter *write = [[SBJsonStreamWriter alloc]init];
[write writeObjectOpen];
[write writeString:@"Testing"];
[write writeObjectClose];

But I don't know how to print this out, and I need to be able to write JSON for my project as I will be updating JSON files, so need to understand how to write JSON.

As anyone used this library before to write? If so could you please show me a quick example of how it is done

Thanks in advance!

Note: I can't use the in built JSON Parser released with new xCode as my app must be able to support phones from IOS 4+ and the new Parser won't work on phones that do not have IOS 5 installed

EDIT

Example say I wanted to create a JSON file which consisted of an array of names eg

{ "name":[ "Elliot Jacobs", "Paul", "Maria", "Richard", "Ana" ] }

EDIT 2:

Example two

{ "HomeScreen":{ "Title":{ "Name":"James Bond", "Number":"07789 123 456" } } }

I'm not quite sure what you mean by 'writing JSON' so I assume that you need to construct a JSON-formatted string. Are you certain that you must use the stream writer? If not, here's an example with strings:

SBJsonWriter *writer = [[SBJsonWriter alloc] init];
NSDictionary *command = [NSDictionary dictionaryWithObjectsAndKeys:
                                @"string1", @"key1",
                                @"string2", @"key2",
                                nil];
NSString *jsonCommand = [writer stringWithObject:command]; // this string will contain the JSON-encoded command NSDictionary

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM