Swift 4 - SwiftyJson比较数组并合并找到的值

时间:2018-01-15 12:39:08

标签: ios arrays json swift

我有一个JSON对象,其中包含名称和值列表。我想与另一个数组[String]进行比较,如果在JSON对象值之一中找到该值,则将该值合并到一个新对象。

我尝试在merge(with)循环中使用for但是,它似乎只合并了一个值。

> example:
> 
>  json_cat = 
>     
>     {
>       name: "aaa",
>       surname: "aaa-1",
>       grade: "aaa-2"
>     },
>     {
>       name: "bbb",
>       surname: "bbb-1",
>       grade: "bbb-2"
>     },
>     {
>       name: "ccc",
>       surname: "ccc-1",
>       grade: "ccc-2"
>     }
>     
//
>     let arraytest:[String] = ["aaa", "ccc", "ddd"]
> 
// array   json_cat compare arraytest
>     result = {
>       name: "aaa",
>       surname: "aaa-1",
>       grade: "aaa-2"
>     },
>     {
>       name: "ccc",
>       surname: "ccc-1",
>       grade: "ccc-2"
>     }
let json_cat: JSON = "json from url" 
let arraytest:[String] = ["aaa", "ccc", "ddd"]
var json_cat_array = JSON()

    json_cat.forEach({ (index, json_p) in
                    if arraytest.contains(json_p["name"].string!) {
                        //print("\(index) - \(json_p)")
                        try! json_cat_array.merge(with: json_p)

                    }
                })

2 个答案:

答案 0 :(得分:0)

此代码使用Swift 4基础JSON解析器。 Swift 4默认情况下有一个奇怪的解析器。

import Foundation
let jsonString = """
        [{
        "surname": "Roxxy",
        "grade": "neaou"
        },
        {
        "name": "Puffi",
        "surname": "Chips",
        "grade": "meaou"
        },
        {
        "name": "Taffy",
        "surname": "Baby",
        "grade": "mew-mew"
        }]
"""
struct JsonCat: Decodable {
    let name: String?
    let surname: String
    let grade: String
}

struct Cat {
    let name: String
    let surname: String
    let grade: String
}


guard let jsonData = jsonString.data(using: .utf8), let jsonCats = try? JSONDecoder().decode([JsonCat].self, from: jsonData) else {
    fatalError()
}

var cats: [Cat] = []
jsonCats.forEach { jsonCat in
    guard let name = jsonCat.name else {
        return
    }
    let cat = Cat(name: name, surname: jsonCat.surname, grade: jsonCat.grade)
    cats.append(cat)
}

print(cats)

答案 1 :(得分:0)

你可以像这样实现你的循环:

json_cat.forEach({ (index, json_p) in
    if arraytest.contains(json_p["name"].string!) {
        //print("\(index) - \(json_p)")

        json_cat_array[json_p["name"].string!] = json_p

    }
})

合并功能实际上是用于合并数据。就像在这个例子中一样:

let original: JSON = [
    "first_name": "John",
    "age": 20,
    "skills": ["Coding", "Reading"],
    "address": [
        "street": "Front St",
        "zip": "12345",
    ]
]

let update: JSON = [
    "last_name": "Doe",
    "age": 21,
    "skills": ["Writing"],
    "address": [
        "zip": "12342",
        "city": "New York City"
    ]
]

let updated = original.merge(with: update)
// [
//     "first_name": "John",
//     "last_name": "Doe",
//     "age": 21,
//     "skills": ["Coding", "Reading", "Writing"],
//     "address": [
//         "street": "Front St",
//         "zip": "12342",
//         "city": "New York City"
//     ]
// ]