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!)
but I need to append new swiftyJSON data to this hoge array .I noticed hoge array doesn't have append property . How should I do that?
Thanks
Steps for Appending to a JSON File In Python, appending JSON to a file consists of the following steps: Read the JSON in Python dict or list object. Append the JSON to dict (or list ) object by modifying it. Write the updated dict (or list ) object into the original file.
Append file creates file if does not exist. But ,if you want to append JSON data first you read the data and after that you could overwrite that data. We have to read the data, parse, append, and then overwrite the original file because of the JSON format.
So using array function all data is stored in array format and then it converts into JSON data with the use of json_encode() function. If the file already exists, we can append it with that old data. file_get_contents() function will get the current data in a file then we can add new data.
JSON_ARRAY_APPEND() – Append Values to a JSON Array in MySQL. When using JSON documents with MySQL, you can use the JSON_ARRAY_APPEND() function to append new values to an array. The way it works is, you provide the JSON document as the first argument, then follow that up with the path to append to, followed by the value to append.
This is a synonym for the JSON document, so the value is appended to the top level array (which in this case, happens to be the only array). Here’s an example of appending a value to an array that’s nested inside another array.
How to push JSON object into array using JavaScript? Answer: If there is a single object and you want to push the whole object into an array then you do simply push the object. If you have multiple objects then do iterate the object.
In the following code, we declare a variable named obj that parses the data from the code.json file. We then “push” the object addition into the notes array. Next, in the same above code, we stringify i.e. create an object into a JSON string. Done! The file has just been written to!
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With