首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过JSON进行排序,查找字符串不同的每个实例

通过JSON进行排序,查找字符串不同的每个实例
EN

Stack Overflow用户
提问于 2018-11-10 22:32:42
回答 1查看 67关注 0票数 0

我试图发现字符串name:的每个实例都是不同的。

至于下面的JSON例子,我想把Alamo草案,House,Lamar和Alamo草案,豪斯里兹,并把他们放在一个数组。

杰森:

代码语言:javascript
复制
[{
"tmsId": "MV011110340000",
"rootId": "15444050",
"subType": "Feature Film",
"title": "Bohemian Rhapsody",
"releaseYear": 2018,
"releaseDate": "2018-11-02",
"titleLang": "en",
"descriptionLang": "en",
"entityType": "Movie",
"genres": ["Biography", "Historical drama", "Music"],
"longDescription": "Singer Freddie Mercury, guitarist Brian May, drummer Roger Taylor and bass guitarist John Deacon take the music world by storm when they form the rock 'n' roll band Queen in 1970. Surrounded by darker influences, Mercury decides to leave Queen years later to pursue a solo career. Diagnosed with AIDS in the 1980s, the flamboyant frontman reunites with the group for Live Aid -- leading the band in one of the greatest performances in rock history.",
"shortDescription": "Singer Freddie Mercury of Queen battles personal demons after taking the music world by storm.",
"topCast": ["Rami Malek", "Lucy Boynton", "Gwilym Lee"],
"directors": ["Bryan Singer"],
"officialUrl": "https://www.foxmovies.com/movies/bohemian-rhapsody",
"ratings": [{
    "body": "Motion Picture Association of America",
    "code": "PG-13"
}],
"advisories": ["Adult Language", "Adult Situations"],
"runTime": "PT02H15M",
"preferredImage": {
    "width": "240",
    "height": "360",
    "uri": "assets/p15444050_v_v5_as.jpg",
    "category": "VOD Art",
    "text": "yes",
    "primary": "true"
},
"showtimes": [{
    {
    "theatre": {
        "id": "9489",
        "name": "Alamo Drafthouse at the Ritz"
    },
    "dateTime": "2018-11-10T19:15",
    "barg": false,
    "ticketURI": "http://www.fandango.com/tms.asp?t=AAUQP&m=185586&d=2018-11-10"
}, {
    "theatre": {
        "id": "9489",
        "name": "Alamo Drafthouse at the Ritz"
    },
    "dateTime": "2018-11-10T22:30",
    "barg": false,
    "ticketURI": "http://www.fandango.com/tms.asp?t=AAUQP&m=185586&d=2018-11-10"
}, {
    "theatre": {
        "id": "5084",
        "name": "Alamo Drafthouse South Lamar"
    },
    "dateTime": "2018-11-10T12:00",
    "barg": false,
    "ticketURI": "http://www.fandango.com/tms.asp?t=AATHS&m=185586&d=2018-11-10"
}, {
    "theatre": {
        "id": "5084",
        "name": "Alamo Drafthouse South Lamar"
    },
    "dateTime": "2018-11-10T15:40",
    "barg": false,
    "ticketURI": "http://www.fandango.com/tms.asp?t=AATHS&m=185586&d=2018-11-10"
},
}]
}]

下面是我的api代码:

代码语言:javascript
复制
var shows = [Shows]()

struct Shows: Codable {
    let showtimes: [Showtimes]

    struct Showtimes: Codable {
    let theatre: Theater

        struct Theater: Codable {
            let id: String
            let name: String
        }

    }
}

func loadShowtimes() {

    let apiKey = ""
    let today = "2018-11-10"
    let zip = "78701"
    let filmId = "MV011110340000"
    let radius = "15"
    let url = URL(string: "http://data.tmsapi.com/v1.1/movies/\(filmId)/showings?startDate=\(today)&numDays=5&zip=\(zip)&radius=\(radius)&api_key=\(apiKey)")
    let request = URLRequest(
        url: url! as URL,
        cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalCacheData,
        timeoutInterval: 10 )

    let session = URLSession (
        configuration: URLSessionConfiguration.default,
        delegate: nil,
        delegateQueue: OperationQueue.main
    )

    let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
        if let data = data {
            do { let shows = try! JSONDecoder().decode([Shows].self, from: data)
                self.shows = shows

            }
        }
    })

    task.resume()

}

如何对数组进行排序并发现name:的每个实例是不同的,然后将每个名称放入一个新的数组中?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-10 23:16:57

有几种方法可以迭代您的Shows数组和它们的Theater数组来获得完整的名称列表。一旦您有了完整的名称列表,您就可以得到这些名称的唯一列表。

以下是一种方法:

代码语言:javascript
复制
let names = Array(Set(shows.map { $0.showtimes.map { $0.theatre.name }}.reduce([]) { $0 + $1 }))

让我们把它分开,以便更好地解释到底发生了什么。

代码语言:javascript
复制
let allNames = shows.map { $0.showtimes.map { $0.theatre.name }}.reduce([]) { $0 + $1 }
let uniqueNames = Array(Set(allNames))

shows.mapshows中遍历每个Shows。内部map依次迭代每个返回其nameShows中的每个Shows中的每个name。因此,内部map给出了一个名称数组。第一个map产生一个名称数组。reduce将这些名称数组合并为一个名称数组,留下allNames和一个包含每个名称的数组。

Array(Set(allNames))的使用首先创建一个唯一的名称集,然后从该集合创建一个数组。

如果希望最终结果按字母顺序排序,那么将.sorted()添加到末尾。

如果您需要保留原来的订单,您可以使用NSOrderedSet并删除对sorted的任何使用。

代码语言:javascript
复制
let names = NSOrderedSet(array: shows.map { $0.showtimes.map { $0.theatre.name }}.reduce([]) { $0 + $1 }).array as! [String]
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53244081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档