如何使用iPhone的JSON框架以编程方式生成JSON [英] How to generate JSON programmatically using JSON framework for iPhone
本文介绍了如何使用iPhone的JSON框架以编程方式生成JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个应用程序,因为我需要向服务器发送JSON以获得一些响应.
I am creating an application in that I need to send a JSON to the server to get some response.
如何使用iPhone的 JSON框架生成JSON?
How to generate JSON using JSON Framework for iPhone?
还有哪些其他可能的方式?
What are the other possible ways?
推荐答案
创建对象的数组或字典,这些对象表示您要通过JSON发送的信息.完成后,将-JSONRepresentation
发送到数组/字典.该方法返回一个JSON字符串,然后将其发送到服务器.
Create an array or dictionary of objects representing the information you want to send via JSON. Having done that, send -JSONRepresentation
to the array/dictionary. That method returns a JSON string, and you send it to the server.
例如:
NSDictionary *o1 = [NSDictionary dictionaryWithObjectsAndKeys:
@"some value", @"key1",
@"another value", @"key2",
nil];
NSDictionary *o2 = [NSDictionary dictionaryWithObjectsAndKeys:
@"yet another value", @"key1",
@"some other value", @"key2",
nil];
NSArray *array = [NSArray arrayWithObjects:o1, o2, nil];
NSString *jsonString = [array JSONRepresentation];
// send jsonString to the server
执行上述代码后,jsonString
包含:
After executing the code above, jsonString
contains:
[
{
"key1": "some value",
"key2": "another value"
},
{
"key1": "yet another value",
"key2": "some other value"
}
]
这篇关于如何使用iPhone的JSON框架以编程方式生成JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文