How to append new data to an existing JSON array(swiftyJSON)

后端 未结 6 1002
梦谈多话
梦谈多话 2021-01-08 00:55

I have an array of SwiftyJson data that I have declared and filled it with data .The code I\'m using to fill the hoge array is this : self.hoge = JSON(data: data!)

相关标签:
6条回答
  • 2021-01-08 01:01

    You can use the merge function for this. https://github.com/SwiftyJSON/SwiftyJSON#merging

    var array: JSON = [1, 2, 3]
    array = try! array.merged(with: [4])
    // -> [1, 2, 3, 4]
    
    0 讨论(0)
  • 2021-01-08 01:11
    let jsonNewJSON:JSON = JSON(newData)
    var arr:[JSON]=jsonOBJ.arrayValue
    arr.append(jsonNewJSON)
    var combinedOBJ = JSON(arr)
    

    I used the above code to append to a swiftyjson array . I first converted the new data to a JSON object. Then I got the array of the existing JSON and append the new JSON. I converted the array back to a JSON object. Admit-ably not great code, but I did not need performance here and it got the job done.

    0 讨论(0)
  • 2021-01-08 01:11

    Declare you main JSON Array as :

    var data:[JSON] = []
    

    If the data source is any other object (like realm) loop over it using for loop But if the data source is another JSON Array perform :

    func setData(){
            data = []
            if let items = sourceData.array {
                for item in items {
                  data.append(item)
                }
            }
            collectionView.reloadData()
        }
    
    0 讨论(0)
  • 2021-01-08 01:20

    Swift 2-4

    Another solution, using Extension

    extension JSON{
        mutating func appendIfArray(json:JSON){
            if var arr = self.array{
                arr.append(json)
                self = JSON(arr);
            }
        }
        
        mutating func appendIfDictionary(key:String,json:JSON){
            if var dict = self.dictionary{
                dict[key] = json;
                self = JSON(dict);
            }
        }
    }
    

    Use:

    var myJSON: JSON = [
        "myDictionary": [String:AnyObject](),
        "myArray" : [1,2,3,4]
    ]
    
    myJSON["myDictionary"].appendIfDictionary(key:"A", json: JSON(["key1":"value1"]))
    myJSON["myDictionary"].appendIfDictionary(key: "B", json: JSON(["key2":"value2"]))
    myJSON["myArray"].appendIfArray(json: JSON(5))
    

    print:

    {
      "myArray" : [
        1,
        2,
        3,
        4,
        5
      ],
      "myDictionary" : {
        "B" : {
          "key2" : "value2"
        },
        "A" : {
          "key1" : "value1"
        }
      }
    }
    
    0 讨论(0)
  • 2021-01-08 01:22

    SwiftyJSON does not have append or extend functionality.

    You can:

    self.hoge = JSON(self.hoge.arrayObject! + JSON(data: newData).arrayObject!)
    

    But I recommend to declare self.hoge as [JSON]

    var hoge:[JSON] = []
    
    func readMoreData() {
    
        let newData: NSData = ...
    
        if let newArray = JSON(data:newData).array {
            self.hoge += newArray
        }
    }
    
    0 讨论(0)
  • 2021-01-08 01:23

    self.hoge should be a Swift Array (which is mutable if declared as var) or casted to a NSMutableArray. If it is of Array type use append. If casted to an NSMutableArray use self.hoge.addObject(yourObject).

    0 讨论(0)
提交回复
热议问题