Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iphone应用程序中使用Json_Iphone_Objective C_Ios - Fatal编程技术网

在iphone应用程序中使用Json

在iphone应用程序中使用Json,iphone,objective-c,ios,Iphone,Objective C,Ios,我试图在我的iphone项目中使用Json, 但我不明白如何在我的项目中开始使用json 帮我摆脱这种状况。 提前谢谢。如果您不喜欢TouchJson,也很好 我使用异步调用我的Web服务。在我的requestFinished方法中,我解析我从调用中收到的JSON,从那里你可以对收到的JSON做几乎任何事情 如果你愿意,你可以从这个开始 本教程将帮助您理解json的作用,但如果您想在代码中开始使用它,则应使用以下示例: April 26, 2009 Dealing with JSON on i

我试图在我的iphone项目中使用Json, 但我不明白如何在我的项目中开始使用json

帮我摆脱这种状况。 提前谢谢。

如果您不喜欢TouchJson,也很好


我使用异步调用我的Web服务。在我的
requestFinished
方法中,我解析我从调用中收到的JSON,从那里你可以对收到的JSON做几乎任何事情

如果你愿意,你可以从这个开始

本教程将帮助您理解json的作用,但如果您想在代码中开始使用它,则应使用以下示例:

April 26, 2009
Dealing with JSON on iPhone
You can easily use the JSON (JavaScript Object Notation) data format in client-server communications 
when writing an iPhone app. This blog is not suggesting that JSON is a more superior format for data 
exchange than its counterparts such as XML. In fact, we have many projects that don't use JSON.
However, handling JSON is relatively straight forward in ObjectiveC.
Unfortunately, Apple iPhone SDK (as of this writing, the latest is iPhone 2.2.1) doesn't come with 
a built-in JSON parser. But I found out a good one called json-framework. It is both a generator 
and a parser. As a generator, json-framework can create JSON data from an NSDictionary. As a parser, 
you can pass to json-framework an NSString that consists of JSON data and it will return a 
NSDictionary that encapsulates the parsed data.

Next, I'm going to show you several examples. Before you proceed, download the library and make 
sure you add it to your SDK path list (see INSTALL file that comes with it). If setup properly, 
you should be able to start using the library by importing its header file:

#import "JSON/JSON.h"

Consider the following code:

    NSDictionary *requestData = [NSDictionary dictionaryWithObjectsAndKeys:
                         @"grio", @"username",
                         @"hellogrio", @"password",
                          nil];

This instantiates a new dictionary which we'll turn into a JSON string. To do so, you'll need to 
use a function called JSONRepresentation. This function is added as a category to NSObject. It 
means that as long as there's an import of JSON.h file, you can call the function on any NSObject 
object.

    NSString* jsonString = [requestData JSONRepresentation];

And this is what you got when you print (NSLog(@"%@", jsonString);):

    {"username":"grio","password":"hellogrio"}

Parsing is just as simple. Consider the following JSON data:

{
    "menu": {
        "id": "file",
        "value": "File",
        "popup": {
            "menuitem": [
            {
                "value": "New",
                "onclick": "CreateNewDoc()"
            },
            {
                "value": "Open",
                "onclick": "OpenDoc()"
            },
            {
                "value": "Close",
                "onclick": "CloseDoc()"
            }
            ]
        }
    }
}

Assume that this is the data that you received from a web service called and is currently stored 
in an NSString called jsonResult. To parse it, you need to create SBJSON object and call one of 
its initialization method, objectWithString.

    SBJSON *json = [[SBJSON new] autorelease];
    NSError *jsonError;
    NSDictionary *parsedJSON = [json objectWithString:jsonResult error:&jsonError];

If parsing fails for reasons such as invalid construct of JSON format, jsonError variable will 
be filled with the error info. If it is successful, parsedJSON will contain keys whose values 
are either an NSString or NSDictionary. Let's look at the inside of parsedJSON:

    NSDictionary* menu = [parsedJSON objectForKey:@"menu"];
    NSLog(@"Menu id: %@", [menu objectForKey:@"id"]);
    NSLog(@"Menu value: %@", [menu objectForKey:@"value"]);

And here's the output:

    Menu id: file
    Menu value: File

Observe the JSON data again. popup is an NSDictionary which has an array of menuitem.

    NSDictionary* popup = [menu objectForKey:@"popup"];
    NSArray* menuItems = [popup objectForKey:@"menuitem"];
    NSEnumerator *enumerator = [menuItems objectEnumerator];
    NSDictionary* item;
    while (item = (NSDictionary*)[enumerator nextObject]) {
        NSLog(@"menuitem:value = %@", [item objectForKey:@"value"]);
    }

And this is the output:

    menuitem:value = New
    menuitem:value = Open
    menuitem:value = Close

抱歉,我忘记了这个网站的链接

发送这个链接,让我了解更多。上面的代码将帮助您理解JSON数据的解析。如果我的应用程序中有视频和youtube,那么如何使用JSON?如果可以发送这个(上面的)教程链接,这将对我有很大帮助,谢谢你现在记住这个链接,如果你知道的话,请给我发链接,这将是非常有帮助的