如何使用新的Swift 3和Alamofire解析JSON(字典和数组)

问题描述 投票:-1回答:2

任何人都可以建议并向我发送用于获取JSON响应的示例代码。很少有API我以NSDictionary和少量API的形式获取数据我以NSArray的形式获取数据。

这是我对Alamofire的API请求。

APIManager.sharedInstance.getTeacherProfileDataFromURL(){(userJson)-> Void in


        let swiftyJsonVar = JSON(userJson)
        print("userJson userJson userJson userJson",userJson)

        print("swiftyJsonVar",swiftyJsonVar)
}

1)具有JSON数据阵列的API

{
    "status": 1,
    “student”: [
        {
            "name": "Sprouse",
            "subject": [
                “english”
            ],
            "personal_email": "",
            "school_email": "[email protected]",
            "phone": "9665478544",
            "class_teacher": false,
            "image": "/assets/default_male.png"
        },
        {
            "name": “elen”,
            "subject": [
                "Social Science"
            ],
            "personal_email": "",
            "school_email": "[email protected]",
            "phone": "9066260799",
            "class_teacher": false,
            "image": "/assets/default_female.png"
        },
            ],
    "message": "Details fetched successfully."
}

2)另一个带有JSON数据字典的API

{
    "status": 1,
    "dashboard": {
        "announcement": [
            {
                "title": "Independence Day Celebration",
                "posted_on": "13 August, 2017"
            }
        ],
        "student": {
            "attendance_percent": 100,
            "assignment": [
                {
                    "title": "Alphabets in HINDI",
                    "end_date": "13/09/2017",
                    "subject": "Hindi",
                    "teacher_name": "Bonnie Bennette"
                }
            ],
            "leave_apply": 13
        },
        "image": "/system/images/86/j1f9DiJi_thumb.jpg?1504593436"
    },
    "message": "Details fetched successfully."
}
ios json swift web-services alamofire
2个回答
0
投票

1)具有JSON数据阵列的API

let swiftyJsonVar = JSON(userJson)
let array : [[String : String]] = swiftyJsonVar["student"]

这将为您提供一系列字典或学生的键值。此外,您可以从数组中获取另一个值

let name : String = array[0]["name"]

2)另一个带有JSON数据字典的API

let swiftyJsonVar = JSON(userJson)
let dictionary : [[String : String]] = swiftyJsonVar["dashboard"]

这将为您提供仪表板的字典或键值数组。

let array : [[String : String]] = dictionary["announcement"]

这将为您提供一系列公告,然后您可以获得更多这样的值

let name : array = array[0]["title"]

0
投票

我已经很久才解决了我的问题,最近发布这是我的答案,这可能对其他人有帮助,这就是我在这里发帖的原因。如果数据是数组的形式。

DispatchQueue.main.async {
            TeacherAPIManager.sharedInstance.FetchTeacherStudentAttendanceDataFromURL(){(attendanceJson)-> Void in

                print("observationInfo --",attendanceJson)
                let swiftyAttendanceJsonVar = JSON(attendanceJson)
                let observation = swiftyAttendanceJsonVar["list"].rawString()!
                let jsonData = observation.data(using: .utf8)!
                let array = try? JSONSerialization.jsonObject(with: jsonData, options: [])  as! Array< Any>

                for observationVar in array!{
                    let totlDic  = NSMutableDictionary()
                    let dic = observationVar as! Dictionary<String,Any>

} }

如果数据是字典的形式。只需在上面的函数数组行中替换下面的行代码。

let dictionary = try? JSONSerialization.jsonObject(with: jsonData, options: [])  as! Dictionary<String, Any>
© www.soinside.com 2019 - 2024. All rights reserved.