如何使用JSON框架为iPhone编程生成JSON

V.V*_*V.V 7 json objective-c iphone-sdk-3.0 ios4 ios

我正在创建一个应用程序,因为我需要将JSON发送到服务器以获得一些响应.

任何人都可以帮我解决如何使用JSON Framework for iPhone 生成JSON的问题吗?

任何其他方式,如果可能的话请建议我或提供一些代码或教程.

我试图找出但无法得到解决方案.

小智 13

创建一个对象的数组或字典,表示您要通过JSON发送的信息.完成后,发送-JSONRepresentation到数组/字典.该方法返回一个JSON字符串,然后将其发送到服务器.

例如:

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
Run Code Online (Sandbox Code Playgroud)

执行上面的代码后,jsonString包含:

[
    {
        "key1": "some value",
        "key2": "another value"
    },
    {
        "key1": "yet another value",
        "key2": "some other value"
    }
]
Run Code Online (Sandbox Code Playgroud)