2

我正在尝试从 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 中的每个值?请帮我。

4

5 回答 5

4

看起来像这样..

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
            }

        }
    }
}
于 2012-06-21T09:31:20.873 回答
1

为此尝试JSONKit。使用起来非常简单。

于 2012-06-23T05:27:35.807 回答
1

我通过使用框架做到了: http: //stig.github.com/json-framework/

它非常强大,可以做不可思议的事情!

在这里,我如何使用它从 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"];

希望这会有所帮助

于 2012-06-21T09:12:58.773 回答
1

查看您的 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];
于 2012-06-21T09:16:07.537 回答
0

请注意这是否仍然相关,但在 iOS 5 中,苹果添加了对 JSON 的合理支持。查看此博客的小教程 无需导入任何 JSON 框架。(如果这个答案是相关的,+1)

于 2012-07-05T19:26:41.870 回答