如何从 xcode 4.2 中的 nsstring 或 byte 获取 JSON 数组数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11134850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the JSON array data from nsstring or byte in xcode 4.2?
提问by user1471568
I'm trying to get values from nsdata class and doesn't work.
我正在尝试从 nsdata 类中获取值并且不起作用。
here is my JSON data.
这是我的 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
}]
}
and here is my code
这是我的代码
-(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;
}
how can I access every value in the JSON? Please help me.
如何访问 JSON 中的每个值?请帮我。
回答by
look like this ..
看起来像这样..
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
}
}
}
}
回答by Edelweiss
I did it by using the framework : http://stig.github.com/json-framework/
我是通过使用框架做到的:http: //stig.github.com/json-framework/
It is very powerfull and can do incredible stuff !
它非常强大,可以做不可思议的事情!
Here how I use it to extract an item name from an HTTP request : (where result is the JSO string)
在这里,我如何使用它从 HTTP 请求中提取项目名称:(其中结果是 JSO 字符串)
NSString *result = request.responseString;
jsonArray = (NSArray*)[result JSONValue]; /* Convert the response into an array */
NSDictionary *jsonDict = [jsonArray objectAtIndex:0];
/* grabs information and display them in the labels*/
name = [jsonDict objectForKey:@"wine_name"];
Hope this will be helpfull
希望这会有所帮助
回答by Mundi
Looking at your JSON, you are not querying the right object in the object hierarchy. The top object, which you extract correctly, is an NSDictionary
. To get at the items array, and the single items, you have to do this.
查看您的 JSON,您没有在对象层次结构中查询正确的对象。您正确提取的顶部对象是NSDictionary
. 要获取 items 数组和单个项目,您必须这样做。
NSArray *items = [iTem objectForKey:@"item"];
NSArray *filteredArray = [items filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:@"id = %d", 2];
if (filteredArray.count) NSDictionary *item2 = [filteredArray objectAtIndex:0];
回答by Steven
Note sure if this is still relevant, but in iOS 5, apple added reasonable support for JSON. Check out this blog for a small TutorialThere is no need to import any JSON framework. (+1 if this answer is relevant)
请注意这是否仍然相关,但在 iOS 5 中,苹果添加了对 JSON 的合理支持。查看此博客的小教程无需导入任何 JSON 框架。(如果此答案相关,则 +1)