Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 522
user970956 Avatar asked Mar 14 '15 06:03

user970956


People also ask

How do I add data to an existing JSON file?

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.

How do you add data to an existing JSON object in node JS?

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.

Which function is used to append data using JSON?

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.

How do I append to a JSON array in MySQL?

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.

Can a value be appended to an array in JSON?

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?

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.

How do I add an object to a JSON string?

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!


1 Answers

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
    }
}
like image 179
rintaro Avatar answered Sep 30 '22 23:09

rintaro