-1

Can any one suggest and send me sample code for fetching JSON response. Few APIs I am getting data in the form of the NSDictionary and few APIs I am getting data in the form of NSArray.

This is my API request with Alamofire.

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


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

        print("swiftyJsonVar",swiftyJsonVar)
}

1) API WITH JSON ARRAY OF DATA

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

2) ANOTHER API WITH JSON DICTIONARY OF DATA

{
    "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."
}
Imad Ali
  • 3,261
  • 1
  • 25
  • 33
jeevan
  • 67
  • 1
  • 13
  • you can use `SwiftyJSON` for this. – Nauman Malik Sep 11 '17 at 08:00
  • @NaumanMalik i am using swiftyjson. i am new ti swift . thats why i couldnt able to hadle json data. can you send sample code – jeevan Sep 11 '17 at 08:14
  • Please read [Correctly parsing JSON in Swift 3](https://stackoverflow.com/questions/39423367/correctly-parsing-json-in-swift-3/39423764#39423764) to learn how to read a JSON string. It's very easy. There is also sample code how to handle arrays and dictionaries – vadian Sep 11 '17 at 08:45
  • @vadian thank you . but i am looking for alamofire req and post – jeevan Sep 12 '17 at 03:53

2 Answers2

0

1) API WITH JSON ARRAY OF DATA

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

This will give you an array of dictionaries or key-values of student. Further you can get another value from the array like that

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

2) ANOTHER API WITH JSON DICTIONARY OF DATA

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

This will give you an array of dictionary or key-value of dashboard.

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

This will give you the array of announcement and next you can get the further values like this

let name : array = array[0]["title"]
Usman Javed
  • 2,437
  • 1
  • 17
  • 26
0

i have resolved my issue long back , posting lately this is my answer it may helpful for other thats why i am posting here. if data is in the form of array.

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>

} }

if data is in the form of dictionary. just replace this below of line code in above function array line.

let dictionary = try? JSONSerialization.jsonObject(with: jsonData, options: [])  as! Dictionary<String, Any>
jeevan
  • 67
  • 1
  • 13