简体   繁体   English

如何使用SwiftyJSON从JSON中获取所选元素并将其打印在控制台上?

[英]How can I get selected elements from my JSON using SwiftyJSON and print it on the console?

I have a webservice that returns data in a selected form: 我有一个Web服务,它以选定的形式返回数据:

[{"_id":"56c861d0bcb42e075f7d47bd",
"username":"Shaylee Thompson",
"photo":"photo.jpg",
"number":"one",
"description":"Aut officia et ipsam. Dolorem pariatur molestiae sed saepe voluptatem voluptatem. Non maiores doloribus quis eum reprehenderit fugit vitae.",
"__v":0,
"updated_at":"2016-02-20T12:53:36.931Z",
"created_at":"2016-02-20T12:53:36.922Z",
"location":{
    "type":"Point",
    "coordinates":[-3.2880974020993934,49.8319259687921]}
},
{"_id":"55a9253297b21080ac8",
"username":"Eve Notter",
"photo":"photo.jpg",
"number":"two",
"description":"Aut officia et ipsam. Dolorem pariatur molestiae sed saepe voluptatem voluptatem. Non maiores doloribus quis eum reprehenderit fugit vitae.",
"__v":0,
"updated_at":"2016-02-20T12:53:36.931Z",
"created_at":"2016-02-20T12:53:36.922Z",
"location":{
    "type":"Point",
    "coordinates":[-2.212142,41.14321231]}
},
etc.

and I want to display in my console the location , username and description from each of the record above. 我想在控制台中显示上述每个记录的locationusernamedescription

I created a class called RestManager.swift that contains two methods: 我创建了一个名为RestManager.swift的类,其中包含两个方法:

static let sharedInstance = RestApiManager()

func getRandomUser(onCompletion: (JSON) -> Void) {
    let route = baseURL
    makeHTTPGetRequest(route, onCompletion: { json, err in
        onCompletion(json as JSON)
    })
}

func makeHTTPGetRequest(path: String, onCompletion: ServiceResponse) {
    let request = NSMutableURLRequest(URL: NSURL(string: path)!)

    let session = NSURLSession.sharedSession()

    let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
        let json:JSON = JSON(data: data!)
        onCompletion(json, error)
        // if I put print(json) then I see the whole json in console
    })
    task.resume()
}

and then in different class I wrote: 然后在不同的课堂上我写道:

override func viewDidLoad() {
    getDummyData()  
}

func getDummyData() {
    RestApiManager.sharedInstance.getRandomUser { json in
        let results = json["username"]
        for (index: String, subJson: JSON) in results {
            let user: AnyObject = JSON["username"].object
            dispatch_async(dispatch_get_main_queue(),{
                print(user) //does not print anything so far
            })
        }
    }
}

So my question is - how should I modify my getDummyData method so that I can see my JSON elements in the console? 所以我的问题是-我应该如何修改getDummyData方法,以便可以在控制台中看到JSON元素?

func getDummyData() {

   RestApiManager.sharedInstance.getRandomUser { json in

        for (index: String, subJson: JSON) in json  {
            let user: AnyObject = subjson["username"].object
            dispatch_async(dispatch_get_main_queue(),{
                print(user) 
            })
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM