- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试从 nsdata 类中获取值但不起作用。
这是我的 JSON 数据。
{
"count": 3,
"item": [{
"id": "1",
"latitude": "37.556811",
"longitude": "126.922015",
"imgUrl": "http://175.211.62.15/sample_res/1.jpg",
"found": false
}, {
"id": "3",
"latitude": "37.556203",
"longitude": "126.922629",
"imgUrl": "http://175.211.62.15/sample_res/3.jpg",
"found": false
}, {
"id": "2",
"latitude": "37.556985",
"longitude": "126.92286",
"imgUrl": "http://175.211.62.15/sample_res/2.jpg",
"found": false
}]
}
这是我的代码
-(NSDictionary *)getDataFromItemList
{
NSData *dataBody = [[NSData alloc] initWithBytes:buffer length:sizeof(buffer)];
NSDictionary *iTem = [[NSDictionary alloc]init];
iTem = [NSJSONSerialization JSONObjectWithData:dataBody options:NSJSONReadingMutableContainers error:nil];
NSLog(@"id = %@",[iTem objectForKey:@"id"]);
//for Test
output = [[NSString alloc] initWithBytes:buffer length:rangeHeader.length encoding:NSUTF8StringEncoding];
NSLog(@"%@",output);
return iTem;
}
如何访问 JSON 中的每个值?请帮助我。
最佳答案
看起来像这样..
NSString *jsonString = @"your json";
NSData *JSONdata = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *jsonError = nil;
if (JSONdata != nil) {
//this you need to know json root is NSDictionary or NSArray , you smaple is NSDictionary
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:JSONdata options:0 error:&jsonError];
if (jsonError == nil) {
//every need check value is null or not , json null like ( "count": null )
if (dic == (NSDictionary *)[NSNull null]) {
return nil;
}
//every property you must know , what type is
if ([dic objectForKey:@"count"] != [NSNull null]) {
[self setCount:[[dic objectForKey:@"count"] integerValue]];
}
if ([dic objectForKey:@"item"] != [NSNull null]) {
NSArray *itemArray = [dic objectForKey:@"item"]; // check null if need
for (NSDictionary *itemDic in itemArray){
NSString *_id = [dic objectForKey:@"id"]; // check null if need
NSNumber *found = (NSNumber *)[dic objectForKey:@"found"];
//.....
//.... just Dictionary get key value
}
}
}
}
关于ios - 如何从 xcode 4.2 中的 nsstring 或 byte 获取 JSON 数组数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11134850/
使用 WSDL2ObjC 我得到了很多类,它们是 NSString 的子类。我发现初始化这些类的任何对象的 NSString 值很麻烦。 假设我的类(class)是这样的: @interface mC
当调用 +[NSURL URLWithString:] 时,我有两个构建 URL 的选项: [[@"http://example.com" stringByAppendingPathComponent
我需要删除字符串末尾的空格。我怎样才能做到这一点?示例:如果字符串是 "Hello " 它必须变成 "Hello" 最佳答案 摘自这里的答案:https://stackoverflow.com/a/5
NSString *test = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 如何将此字符串转换为字节? 最佳答案 NSData *bytes = [test dataUsingEn
我对 NSString 变量有疑问。 .h 文件 NSString *strDeleteFilePath; @property (nonatomic,retain) NSString*
我对这个功能有疑问: - (instancetype)initWithCenter:(CLLocationCoordinate2D)center
如果我有方法 - (void) myMethod:(NSString *)string { [Object anothermethodWithString:string]; } 我打电话 [O
ios 中初始化字符串变量的两种方式有什么区别(,adv/disadv)? NSString *var = @"value" 和 NSString *var =[ [NSString alloc] i
我认为这个问题已经足够清楚了,但仍然 - 有什么区别: NSString *string = @"Hello world!"; 和 NSString *string = [[NSString allo
我正在尝试将 NSString 转换为单个字符。当我写 [[NSLocale] currentLocale] objectForKey:NSLocaleDecimalSeparator] 时,我得到
我有指示制作一个前缀方法,该方法在每个位置采用两个字符串,其中 mask = 0 并且第一个字符串 = 第二个字符串,直到不满足这些条件为止,即您的前缀 NSString。 我做了尝试,但由于某种原因
我有 3 个 NSString 和 1 个带值的 NSArray,我尝试将所有字符串转换为一个: string01 = [string01 stringByAppendingString:[numbe
我有一个包含以下文本的 NSString: You can now download "file.png" from "http://www.example.com/RANDOM.png" 如何使用该
我有一个完整的 URL,比方说 http://www.mywebsite.com//Folder /Detals /Final Image /La Image Logo.jpg 在这个NSString
在某些情况下,我可能有一个包含多个单词的 NSString,其中一些是重复的。我想要做的是采用如下所示的字符串: One Two Three Three Three Two Two Two One O
所以我有一个 NSString,让我们说:NSString *mainString = @"My Name is Andrew (I like computers)";我想从 mainString 中
这是我的职责。首先 println 将正确的散列打印到控制台,但在下一行程序崩溃。你能帮助我吗? func sha256(string: NSString) -> NSString { var
我刚刚看到一行由同事编写的代码,我以前从未在 Obj-C 中见过它。它看起来形式很糟糕,我想更改它,但它似乎有效(应用程序按预期工作),我什至不知道将其更改为什么。 NSString **string
我花了很长时间寻找一种获取 nsstring 对象的匹配计数的方法。但我找不到。如何获取 String_one 和 String_Two 的匹配计数?我需要你的帮助.. NSString *Strin
在我看来,我正在处理的代码应该是这样写的: if (!instructions) { instructions = [[UITextView alloc] init]; ins
我是一名优秀的程序员,十分优秀!