【问题标题】:how to fetch data using json parsing in ios and show result in another page?如何在 ios 中使用 json 解析获取数据并在另一个页面中显示结果?
【发布时间】:2016-07-15 10:56:30
【问题描述】:

我想从一个网站获取数据,该网站将采用 json 格式,并希望在我的应用程序的第二页中显示它。我如何在ios中解析它? json 响应会像这样:

{
    "response_code": 200,
    "error": false,
    "train_name": "KCG YPR EXP",
    "train_num": "17603",
    "pnr": "1234567890",
    "failure_rate": 19.346153846153847,
    "doj": "20-8-2015",
    "chart_prepared": "Y",
    "class": "SL",
    "total_passengers": 2,
    "train_start_date": {
        "month": 8,
        "year": 2015,
        "day": 20
    },
    "from_station": {
        "code": "KCG",
        "name": "KACHEGUDA"
    },
    "boarding_point": {
        "code": "KCG",
        "name": "KACHEGUDA"
    },
    "to_station": {
        "code": "YPR",
        "name": "YESVANTPUR JN"
    },
    "reservation_upto": {
        "code": "YPR",
        "name": "YESVANTPUR JN"
    },
    "passengers": [
        {
            "no": 1,
            "booking_status": "S7,58,GN",
            "current_status": "S7,58",
            "coach_position": 9
        },
        {
            "no": 2,
            "booking_status": "S7,59,GN",
            "current_status": "S7,59",
            "coach_position": 9
        }
    ]
}

【问题讨论】:

标签: ios iphone json


【解决方案1】:
NSError *err;
    NSURL *url=[NSURL URLWithString:@"your url"];
    NSURLRequest *req=[NSURLRequest requestWithURL:url];
    NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:&err];
    NSDictionary *json=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

【讨论】:

  • sendSynchronousRequest 不是一个好建议。所有网络通信都应该异步工作。
  • @vadian 我知道,但这只是演示代码。他可以以任何他想实现的方式使用它。
猜你喜欢
  • 2021-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-13
  • 2012-05-01
  • 2021-02-21
  • 2021-05-27
  • 1970-01-01
相关资源
最近更新 更多