Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.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
在objective-c中为我的iphone应用解析json_Iphone_Objective C_Json - Fatal编程技术网

在objective-c中为我的iphone应用解析json

在objective-c中为我的iphone应用解析json,iphone,objective-c,json,Iphone,Objective C,Json,我在解析iPhone应用程序的json数据时遇到问题,我是objective-C新手。我需要解析json并获取值才能继续。请帮忙。这是我的JSON数据: [{"projId":"5","projName":"AdtvWorld","projImg":"AdtvWorld.png","newFeedCount":"0"},{"projId":"1","projName":"Colabus","projImg":"Colabus.png","newFeedCount":"0"},{"projId":

我在解析iPhone应用程序的json数据时遇到问题,我是objective-C新手。我需要解析json并获取值才能继续。请帮忙。这是我的JSON数据:

[{"projId":"5","projName":"AdtvWorld","projImg":"AdtvWorld.png","newFeedCount":"0"},{"projId":"1","projName":"Colabus","projImg":"Colabus.png","newFeedCount":"0"},{"projId":"38","projName":"Colabus Android","projImg":"ColabusIcon.jpg","newFeedCount":"0"},{"projId":"25","projName":"Colabus Internal Development","projImg":"icon.png","newFeedCount":"0"},{"projId":"26","projName":"Email Reply Test","projImg":"","newFeedCount":"0"},{"projId":"7","projName":"PLUS","projImg":"7plusSW.png","newFeedCount":"0"},{"projId":"8","projName":"Stridus Gmail Project","projImg":"scr4.png","newFeedCount":"0"}]
我建议使用库进行解析

这里有一个关于如何使用它的例子。 基本上,您将得到一个字典,并使用带有键的
objectForKey
来检索值。

我建议使用库进行解析

这里有一个关于如何使用它的例子。
基本上,您将得到一个字典,并使用带有键的
objectForKey
来检索值。

在iOS 5或更高版本上,您可以使用
NSJSONSerialization
。如果您的JSON数据是字符串,则可以执行以下操作:

NSError *e = nil;
NSData *data = [stringData dataUsingEncoding:NSUTF8StringEncoding];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e];
编辑以获取特定值:

NSDictionary *firstObject = [jsonArray objectAtIndex:0];
NSString *projectName = [firstObject objectForKey:@"projName"];

在iOS 5或更高版本上,您可以使用
NSJSONSerialization
。如果您的JSON数据是字符串,则可以执行以下操作:

NSError *e = nil;
NSData *data = [stringData dataUsingEncoding:NSUTF8StringEncoding];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e];
编辑以获取特定值:

NSDictionary *firstObject = [jsonArray objectAtIndex:0];
NSString *projectName = [firstObject objectForKey:@"projName"];
我已经成功地使用json进行了读写

请看一下,并了解如何使用它

本质上,对于解析,只需将字符串提供给SBJsonParser,它就会返回一个带有objectForKey函数的字典。例如,您的代码可能类似于:

NSDictionary* parsed = [[[SBJsonParser alloc] init] objectWithString: json];
NSString* projId = [parsed objectForKey:@"projId"];
我已经成功地使用json进行了读写

请看一下,并了解如何使用它

本质上,对于解析,只需将字符串提供给SBJsonParser,它就会返回一个带有objectForKey函数的字典。例如,您的代码可能类似于:

NSDictionary* parsed = [[[SBJsonParser alloc] init] objectWithString: json];
NSString* projId = [parsed objectForKey:@"projId"];
使用SBJson

SBJsonParser *parser = [[SBJsonParser alloc] init];

NSMutableDictionary *dicRes = [parser objectWithString:stringFromServer error:nil];
使用SBJson

SBJsonParser *parser = [[SBJsonParser alloc] init];

NSMutableDictionary *dicRes = [parser objectWithString:stringFromServer error:nil];

不需要使用第三方类。Objective-c已经包括处理JSON

NSJSONSerialization
需要一个
NSData
对象或从URL读取。使用JSON字符串测试了以下内容:

NSString *json; // contains your example with escaped quotes
NSData *jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData 
    options:NSJSONReadingAllowFragments error:&error] 

有关
NSJSONSerialization
的更多选项,请参阅

无需使用第三方类。Objective-c已经包括处理JSON

NSJSONSerialization
需要一个
NSData
对象或从URL读取。使用JSON字符串测试了以下内容:

NSString *json; // contains your example with escaped quotes
NSData *jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData 
    options:NSJSONReadingAllowFragments error:&error] 
有关
NSJSONSerialization
的更多选项,请参阅

NSJSONSerialization(iOS 5.0或更高版本)


NSJSONSerialization(iOS 5.0或更高版本)

查看此答案查看此答案我尝试过,但如何获取单个键的值,如“projName”的值我尝试过,但如何获取单个键的值,如“projName”的值