如何使用JSON框架为iPhone编程生成JSON [英] How to generate JSON programatically using JSON framework for iPhone

查看:146
本文介绍了如何使用JSON框架为iPhone编程生成JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating an application in that I need to send a JSON to the server to get some response.

任何人都可以帮助我如何使用JSON生成JSON适用于iPhone的 JSON Framework ??

Can any one help me out how to generate JSON using JSON Framework for iPhone ??

其他如果可能的话,请建议我或提供一些代码或教程。

Any other way if possible then please suggest me or provide me some code or tutorial.

我试图找出但无法获得解决方案。

I tried to find out but can not get the solutions.

推荐答案

创建一个对象的数组或字典,表示您要通过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"
    }
]

这篇关于如何使用JSON框架为iPhone编程生成JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆