0
votes

I'm new in Swift3 and I'm confused how to get value from this JSON array.

This is my coding:

let url = URL(string: apiServer)
URLSession.shared.dataTask(with: url!) {(data, response, error) in
    if error != nil {
       return
    }else{
        do {
            let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: Any]

            if let allEntry = json?[“allentry"] as? [NSArray] {
                 print("All Entry: \(allEntry)")
             }
        }catch{
            print(error.localizedDescription)
        }
    }
}.resume()

This is JSON:

"allentry": [
              [
                "Month",
                "Entry"
              ],
              [
                "Jan",
                109.52380952380953
              ],
              [
                "Feb",
                105.26315789473684
              ],
              [
                "Mar",
                104.54545454545455
              ],
              [
                "Apr",
                100
              ],
              [
                "May",
                105
              ],
              [
                "Jun",
                106.66666666666667
              ],
              [
                "Jul",
                100
              ],
              [
                "Aug",
                100
              ],
              [
                "Sep",
                100
              ],
              [
                "Oct",
                72.727272727272734
              ],
              [
                "Nov",
                0
              ],
              [
                "Dec",
                0
              ]
            ]

The output I got was this:

All Entry:

[<__NSArrayI 0x170029e60>( Month, Entry ) , <__NSArrayI 0x17002a0a0>( Jan, 109.5238095238095 ) , <__NSArrayI 0x170029fc0>( Feb, 105.2631578947368 ) , <__NSArrayI 0x17002a100>( Mar, 104.5454545454545 ) , <__NSArrayI 0x17002a2e0>( Apr, 100 ) , <__NSArrayI 0x17002a3a0>( May, 105 ) , <__NSArrayI 0x17002a740>( Jun, 106.6666666666667 ) , <__NSArrayI 0x17002a920>( Jul, 100 ) , <__NSArrayI 0x17002a860>( Aug, 100 ) , <__NSArrayI 0x17002ade0>( Sep, 100 ) , <__NSArrayI 0x17002a8c0>( Oct, 72.72727272727273 ) , <__NSArrayI 0x17002a980>( Nov, 0 ) , <__NSArrayI 0x17002a9e0>( Dec, 0 ) ]

2
Unrelated but are you responsible for the creation of the JSON? If yes consider to use one dictionary rather than thirteen arrays.vadian

2 Answers

0
votes

The JSON is an array of arrays, when you are printing it in the console, the console is printing the arrays inside the surrounding array.

As to how can you access the individual arrays and the values in them, you can use loops like this:

var months = [Any]()
var values = [Any]()
for entry in allEntry {
    months.append(entry[0])
    values.append(entry[1])
}
0
votes

This is a solution which maps the nested JSON arrays into a custom struct.
The first entry with the field names is being dropped.

struct Entry {
    let month : String
    let entry : Double
}

let url = URL(string: apiServer)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
    if error != nil {
        print(error!)
        return
    } else {
        do {
            if let json = try JSONSerialization.jsonObject(with: data!) as? [String:Any],
                let entries = json["allentry"] as? [[Any]] {
                let items = entries.dropFirst().map{ Entry(month: $0[0] as! String, entry: $0[1] as! Double) }
                print(items)
            }
        } catch {
            print(error.localizedDescription)
        }
    }
}.resume()