简体   繁体   English

使用SBJSON编写JSON

[英]Writing JSON using SBJSON

I have recently started parsing JSON documents using SBJSON Parser, and I am able to read JSON documents just fine. 我最近开始使用SBJSON Parser解析JSON文档,我能够很好地读取JSON文档。 However I am unable to figure out how I am meant to write JSON using this library. 但是我无法弄清楚我是如何使用这个库编写JSON的。 Under the documentation 根据文件

http://stig.github.com/json-framework/api/3.0/interfaceSBJsonStreamWriter.html 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. 有一个用于编写JSON的类,但我无法弄清楚如何使用它。 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. 但我不知道如何打印出来,我需要能够为我的项目编写JSON,因为我将更新JSON文件,因此需要了解如何编写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 注意:我无法使用随新版xCode发布的内置JSON Parser,因为我的应用程序必须能够支持来自IOS 4+的手机,并且新的Parser将无法在未安装IOS 5的手机上运行

EDIT 编辑

Example say I wanted to create a JSON file which consisted of an array of names eg 示例说我想创建一个由一系列名称组成的JSON文件,例如

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

EDIT 2: 编辑2:

Example two 例二

{ "HomeScreen":{ "Title":{ "Name":"James Bond", "Number":"07789 123 456" } } } {“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. 我不太确定'编写JSON'是什么意思所以我假设你需要构造一个JSON格式的字符串。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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