使用Objective-C解析json [英] parse json using objective-c

查看:121
本文介绍了使用Objective-C解析json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Objective-C解析JSON的帮助

i need help parsing json using objective-c

这是我的json:

{
    "days": [
        {
            "reference": "13L6-A67-1",
            "period": "",
            "dayinit": "4",
            "hourinit": "9",
            "minuteinit": "30",
            "dayend": "4",
            "hourend": "10",
            "minuteend": "30",
            "totalattendance": "5",
            "currentattendance": "5",
            "teacher_surname": "jones",
            "teacher_forenames": "Carol",
            "room": "C109"
        },
        {
            "reference": "13NAPUSD-A1",
            "period": "",
            "dayinit": "3",
            "hourinit": "10",
            "minuteinit": "45",
            "dayend": "3",
            "hourend": "11",
            "minuteend": "45",
            "totalattendance": "3",
            "currentattendance": "3",
            "teacher_surname": "Carol",
            "teacher_forenames": "Nicola",
            "room": "M139"
        },
        {
            "reference": "13NASUWO-X1",
            "period": "",
            "dayinit": "3",
            "hourinit": "14",
            "minuteinit": "40",
            "dayend": "3",
            "hourend": "15",
            "minuteend": "5",
            "totalattendance": "0",
            "currentattendance": "0",
            "teacher_surname": "",
            "teacher_forenames": "",
            "room": ""
        },
        {
            "reference": "13NASUWO-X1",
            "period": "",
            "dayinit": "5",
            "hourinit": "13",
            "minuteinit": "35",
            "dayend": "5",
            "hourend": "14",
            "minuteend": "0",
            "totalattendance": "0",
            "currentattendance": "0",
            "teacher_surname": "",
            "teacher_forenames": "",
            "room": ""
        }]}

然后,我需要将其过滤到单独的数组中,以用于不同的"Dayinit"键. 我只是不知道从哪里开始.

then i need to filter it into separate arrays for the different "Dayinit" keys. I just don't know where to start on this one.

到目前为止,我只在youtube上找到了解析json的关键视频,但没有一个视频展示了解析后如何过滤数据

So far i have only found a key videos on youtube on parsing json but none of them show how to filter the data after it has been parsed

推荐答案

,您需要根据json获取密钥. 使用该键,您将获得数据.

you need to get the key as per your json. using that keys you will get the data.

 NSURL * url=[NSURL URLWithString:@"http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo"];   // pass your URL  Here.

        NSData * data=[NSData dataWithContentsOfURL:url];

        NSError * error;

        NSMutableDictionary  * json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];

        NSLog(@"%@",json);


        NSMutableArray * referanceArray=[[NSMutableArray alloc]init];

        NSMutableArray * periodArray=[[NSMutableArray alloc]init];

        NSArray * responseArr = json[@"days"];

        for(NSDictionary * dict in responseArr)
        {

            [referanceArray addObject:[dict valueForKey:@"reference"]];
            [periodArray addObject:[dict valueForKey:@"period"]];

        }


        NSLog(@"%@",referanceArray);   // Here you get the Referance data
        NSLog(@"%@",periodArray);      // Here you get the Period data

您在这里使用

[referanceArray addObject:[dict valueForKey:@"reference"]];

使用所有这些键来获取数据.

used all this keys to get data.

尝试此代码.

这篇关于使用Objective-C解析json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆