如何遍历对象数组并创建json对象?

时间:2019-06-27 13:48:23

标签: json swift

我有一个300个对象的数组,每个对象包含许多参数,我想遍历该数组以创建仅包含两个参数的JSON对象。我的对象是通过数组中的json结构创建的:

 "publicationId": 13,
            "name": null,
            "code": "FIGA_20190516",
            "parutionDate": "2019-05-16",
            "quadri": "FIGA",
            "ratio": null,
            "numero": "0",
            "createdAt": "2019-06-18 21:29:21",
            "nbPages": 0,
            "search": 1,
            "down": 1,
            "thumb_height": 0,
            "quartPageWidth": 0,
            "quartPageHeight": 0,
            "pdfHeight": "0",
            "pdfWidth": "0",
            "PDF": "2019-06-18 21:29:21",
            "XML": null,
            "SendKM": "2019-06-18 21:29:21",
            "KM": null,
            "Melody": null,
            "filename": null,
            "SendNormalize": null,
            "Normalized": null,
            "librelioSubTitle": null,
            "SendProcessArticle": null,
            "ProcessedArticles": null,
            "melodyCallBackUrl": null,
            "htmlMilibris": false,
            "htmlHarry": false,
            "sendProcessLibrelio": null,
            "processedLibrelio": null,
            "id": 77066,
            "_solrDocument": {},
            "_repository": "FIGA/2019/05/FIGA_20190516/"

,我想遍历数组以使用此结构创建JSON对象:

"availableParutions" : {
        id : createdAt,
        id : createdAt,
        id : createdAt
    }

非常感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

您可以尝试以下代码:

注意:我已经创建了自己的json obj以用于评估,您可以直接使用json。

    let json = [
        [
        "publicationId":11,
        "createdAt":"2019-06-18 21:29:21",
        "id":77063,
        ],
        [
            "publicationId":12,
            "createdAt":"2019-06-18 21:29:21",
            "id":77065,
        ],
        [
            "publicationId":13,
            "createdAt":"2019-06-18 21:29:21",
            "id":77066,
        ]
        ]

 var jsonArr: [[String:Any]] = [[String:Any]]()
        for obj in json {
            let id = obj["id"] as! Int
            let jsonObj = [
                "\(id)": obj["createdAt"] as! String
            ]
            jsonArr.append(jsonObj as [String : Any])
        }
        print(jsonArr)

答案 1 :(得分:1)

您可以创建一个新结构,以表示您想要的Json输出中的数据

struct NewStruct: Encodable {
    let id: Int
    let createAt: String
}

此结构符合Encodable,因此可以将其编码为json,但由于它非常简单,因此不需要额外的编码

然后,您可以使用NewStruct将大对象数组转换为map数组

let newArray = array.map { NewStruct(id: $0.id, createAt: $0.createdAt) }

如果我理解正确,则此数组应为名为“ availableParutions”的属性,因此让我们为json创建外部结构

struct Output: Codable {
    let availableParutions: [NewStruct]
}

并加载

let output = Output(availableParutions: newArray)

对其进行编码

do {
    let jsonData = try JSONEncoder().encode(output)
} catch {
    print(error)
}

答案 2 :(得分:0)

您可以创建一个空字典,然后遍历json数组并取出idcreatedAt。使用idcreatedAt更新字典。最后,将该词典设置为新词典中availableParutions的值。

代码:

    var map: [Int: Any]  = [:]
    for item in json {
        let id = item["id"] as! Int
        map[id] = item["createdAt"]!
    }
    let outerObject = ["availableParutions": map]
    print(outerObject)