本文介绍了使用swift在领域中添加json数组(包含内部数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了生成JSON的API,该JSON看起来像这样

I have created API which generate JSON, that JSON looks like this

[
  {
    "id": 105,
    "date": "2018-09-06T22:37:57",
    "date_gmt": "2018-09-06T17:07:57",
    "guid": {
      "rendered": "http://wh2.6ae.myftpupload.com/?p=105"
    },
    "modified": "2018-09-06T22:37:57",
    "modified_gmt": "2018-09-06T17:07:57",
    "slug": "video-post-20",
    "status": "publish",
    "type": "post",
    "link": "http://wh2.6ae.myftpupload.com/video-post-20/",
    "title": {
      "rendered": "Video post – 20"
    },
    "content": {
      "rendered": "",
      "protected": false
    },
    "excerpt": {
      "rendered": "",
      "protected": false
    },
    "author": 1,
    "featured_media": 0,
    "comment_status": "open",
    "ping_status": "open",
    "sticky": false,
    "template": "",
    "format": "video",
    "meta": [

    ],
    "categories": [
      5
    ],
    "tags": [

    ],
    "fimg_url": false,
    "acf": {
      "imagesCarousel": false,
      "youtube_video_id": "mwzExNYs12Y",
      "upload_video": false
    },
    "_links": {
      "self": [
        {
          "href": "http://wh2.6ae.myftpupload.com/wp-json/wp/v2/posts/105"
        }
      ],
      "collection": [
        {
          "href": "http://wh2.6ae.myftpupload.com/wp-json/wp/v2/posts"
        }
      ],
      "about": [
        {
          "href": "http://wh2.6ae.myftpupload.com/wp-json/wp/v2/types/post"
        }
      ],
      "author": [
        {
          "embeddable": true,
          "href": "http://wh2.6ae.myftpupload.com/wp-json/wp/v2/users/1"
        }
      ],
      "replies": [
        {
          "embeddable": true,
          "href": "http://wh2.6ae.myftpupload.com/wp-json/wp/v2/comments?post=105"
        }
      ],
      "version-history": [
        {
          "count": 1,
          "href": "http://wh2.6ae.myftpupload.com/wp-json/wp/v2/posts/105/revisions"
        }
      ],
      "predecessor-version": [
        {
          "id": 111,
          "href": "http://wh2.6ae.myftpupload.com/wp-json/wp/v2/posts/105/revisions/111"
        }
      ],
      "wp:attachment": [
        {
          "href": "http://wh2.6ae.myftpupload.com/wp-json/wp/v2/media?parent=105"
        }
      ],
      "wp:term": [
        {
          "taxonomy": "category",
          "embeddable": true,
          "href": "http://wh2.6ae.myftpupload.com/wp-json/wp/v2/categories?post=105"
        },
        {
          "taxonomy": "post_tag",
          "embeddable": true,
          "href": "http://wh2.6ae.myftpupload.com/wp-json/wp/v2/tags?post=105"
        }
      ],
      "curies": [
        {
          "name": "wp",
          "href": "https://api.w.org/{rel}",
          "templated": true
        }
      ]
    }
  }
]

我为此创建了类

  • 邮政课

  • Post Class

import Foundation
import RealmSwift

class Post : Object{

@objc dynamic var id = 0
@objc dynamic var date = ""
@objc dynamic var date_gmt = ""
@objc dynamic var modified = ""
@objc dynamic var modified_gmt = ""
@objc dynamic var slug = ""
@objc dynamic var status = ""
@objc dynamic var type = ""
@objc dynamic var link = ""
@objc dynamic var format = ""
@objc dynamic var fimg_url : Bool = false

@objc dynamic var title : Rendered? = nil
@objc dynamic var excerpt : Rendered? = nil
@objc dynamic var content : Rendered? = nil
@objc dynamic var acf : Acf? = nil


override static func primaryKey() -> String? {
    return "id"
}
}

  • ImageCarousel

  • ImageCarousel

    import Foundation
    import RealmSwift
    
    class ImagesCarousel: Object {
    
    @objc dynamic var ID = 0
    @objc dynamic var id = 0
    @objc dynamic var title = ""
    @objc dynamic var filename = ""
    @objc dynamic var filesize  = 0
    @objc dynamic var url = ""
    @objc dynamic var date = ""
    @objc dynamic var modified = ""
    @objc dynamic var mime_type = ""
    @objc dynamic var type = ""
    @objc dynamic var subtype = ""
    @objc dynamic var width = 0
    @objc dynamic var height = 0
    @objc dynamic var sizes : Sizes? = nil
    
    override static func primaryKey() -> String? {
        return "ID"
    }
    }
    

  • Acf

  • Acf

    import Foundation
    import RealmSwift
    
    class Acf: Object {
    
    private RealmList<ImagesCarousel> imagesCarousel;
    var imagesCarousel = List<ImagesCarousel>()
    @objc dynamic var youtube_video_id = ""
    
    }
    

  • 已渲染

  • Rendered

    import Foundation
    import RealmSwift
    
    class Rendered: Object {
    
    @objc dynamic var rendered = ""
    
    }
    

  • 大小

  • Size

    import Foundation
    import RealmSwift
    import Realm
    
    class Sizes: Object {
    @objc dynamic var large = ""
    }
    

  • 我的快捷代码

    if let allData = response.result.value as? NSArray{
        let realm = try! Realm()
        for selectedData in allData{
            print(selectedData)
    
            let jsonData = try? JSONSerialization.data(withJSONObject: selectedData, options: [])
    
            let myString = String(data: jsonData!, encoding: String.Encoding.utf8)
            print(myString)
    
            // Insert from Data containing JSON
            try! realm.write {
    
                let json = try! JSONSerialization.jsonObject(with: data!, options: [])
                realm.create(Post.self, value: json, update: true)
                //realm.add(testData.self, value: json, update: true)
            }
        }
    }
    

    这是给我这种类型的错误

    Which is giving me this type of error

    [__NSCFDictionary longLongValue]: unrecognized selector sent to
     instance 0x7fdcc8785820 2016-07-06 10:25:30.090
     mydrawing[9436:2732447] *** Terminating app due to uncaught exception
     'NSInvalidArgumentException', reason: '-[__NSCFDictionary
     longLongValue]: unrecognized selector sent to instance 0x7fdcc8785820'
    

    但是如果我从json数据中删除[和],则可以正常工作

    but if I remove [ and ] from json data then it's working fine

    let data = "{\"id\":1088,\"date\":\"2018-09-06T22:37:57\",\"date_gmt\":\"2018-09-06T17:07:57\",\"guid\":{\"rendered\":\"http:\\/\\/wh2.6ae.myftpupload.com\\/?p=105\"},\"modified\":\"2018-09-06T22:37:57\",\"modified_gmt\":\"2018-09-06T17:07:57\",\"slug\":\"video-post-20\",\"status\":\"publish\",\"type\":\"post\",\"link\":\"http:\\/\\/wh2.6ae.myftpupload.com\\/video-post-20\\/\",\"title\":{\"rendered\":\"Video post &#8211; 20\"},\"content\":{\"rendered\":\"\",\"protected\":false},\"excerpt\":{\"rendered\":\"\",\"protected\":false},\"author\":1,\"featured_media\":0,\"comment_status\":\"open\",\"ping_status\":\"open\",\"sticky\":false,\"template\":\"\",\"format\":\"video\",\"meta\":\"m\",\"categories\":5,\"tags\":\"m\",\"fimg_url\":false,\"_links\":{\"self\":{\"href\":\"http:\\/\\/wh2.6ae.myftpupload.com\\/wp-json\\/wp\\/v2\\/posts\\/105\"},\"collection\":{\"href\":\"http:\\/\\/wh2.6ae.myftpupload.com\\/wp-json\\/wp\\/v2\\/posts\"},\"about\":{\"href\":\"http:\\/\\/wh2.6ae.myftpupload.com\\/wp-json\\/wp\\/v2\\/types\\/post\"},\"author\":{\"embeddable\":true,\"href\":\"http:\\/\\/wh2.6ae.myftpupload.com\\/wp-json\\/wp\\/v2\\/users\\/1\"},\"replies\":{\"embeddable\":true,\"href\":\"http:\\/\\/wh2.6ae.myftpupload.com\\/wp-json\\/wp\\/v2\\/comments?post=105\"},\"version-history\":{\"count\":1,\"href\":\"http:\\/\\/wh2.6ae.myftpupload.com\\/wp-json\\/wp\\/v2\\/posts\\/105\\/revisions\"},\"predecessor-version\":{\"id\":111,\"href\":\"http:\\/\\/wh2.6ae.myftpupload.com\\/wp-json\\/wp\\/v2\\/posts\\/105\\/revisions\\/111\"},\"wp:attachment\":{\"href\":\"http:\\/\\/wh2.6ae.myftpupload.com\\/wp-json\\/wp\\/v2\\/media?parent=105\"},\"wp:term\":{\"taxonomy\":\"category\",\"embeddable\":true,\"href\":\"http:\\/\\/wh2.6ae.myftpupload.com\\/wp-json\\/wp\\/v2\\/categories?post=105\"},\"curies\":{\"name\":\"wp\",\"href\":\"https:\\/\\/api.w.org\\/{rel}\",\"templated\":true}}}".data(using: .utf8)
    
    let realm = try! Realm()
    
    // Insert from Data containing JSON
    try! realm.write {
    
    let json = try! JSONSerialization.jsonObject(with: data!, options: [])
    realm.create(Post.self, value: json, update: true)
    }
    

    • 我引用了此链接
      • 最后,我认为我无法在领域中添加数组,也没有找到任何官方方法.
      • 推荐答案

        您的代码只是尝试替换此行

        You'r code worked just try to replace this line

          try! realm.write {
        
                    let json = try! JSONSerialization.jsonObject(with: data!, options: [])
                    realm.create(Post.self, value: json, update: true)
                    //realm.add(testData.self, value: json, update: true)
                }
        

        对此

          try! realm.write {
        
                    let json = try! JSONSerialization.jsonObject(with:  jsonData!, options: [])
                    realm.create(Post.self, value: json, update: true)
                    //realm.add(testData.self, value: json, update: true)
                }
        

        结果

        这篇关于使用swift在领域中添加json数组(包含内部数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-14 04:24