使用SBJSON编写JSON [英] Writing JSON using SBJSON
问题描述
我最近开始使用SBJSON Parser解析JSON文档,我能够很好地读取JSON文档。但是我无法弄清楚我是如何使用这个库编写JSON的。在文件下
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
有一个用于编写JSON的类,但是我无法弄清楚如何使用它。他的文档中没有关于如何使用它的教程,我在网上找不到关于使用它的任何教程。
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];
但我不知道如何打印出来,我需要能够写JSON对于我的项目,我将更新JSON文件,因此需要了解如何编写JSON。
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
提前致谢!
注意:我无法使用随新版xCode发布的内置JSON Parser,因为我的应用程序必须能够支持来自IOS 4+的手机,并且新的Parser将无法在未安装IOS 5的手机上运行
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
编辑
示例说我想创建一个由一系列名称组成的JSON文件,例如
Example say I wanted to create a JSON file which consisted of an array of names e.g.
{
name:[
Elliot Jacobs,
Paul,
Maria,
Richard ,
Ana
]
}
{ "name":[ "Elliot Jacobs", "Paul", "Maria", "Richard", "Ana" ] }
编辑2:
示例二
{
HomeScreen:{
Title:{
Name:James Bond,
数字:07789 123 456
}
}
}
{ "HomeScreen":{ "Title":{ "Name":"James Bond", "Number":"07789 123 456" } } }
推荐答案
我不太清楚'写JSON'是什么意思所以我假设你需要构造一个JSON格式的字符串。你确定你必须使用流编写器吗?如果没有,这是一个带字符串的例子:
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
这篇关于使用SBJSON编写JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!