0

I have a json array with the following structure

{
        "connection": {
            "established": "yes"
        },
        "ping": {
            "server": "thirteen"
        }
    }

also i have a simple array of server names called servers

my goal is to append the values from the servers array to the Jsonarray - copying the "connection", "established", "yes", "ping", "server" values and just modifying the "thirteen" value.

So that the end result would look something like this

{
        "connection": {
            "established": "yes"
        },
        "ping": {
            "server": "fourteen"
        }
    }, 
{
        "connection": {
            "established": "yes"
        },
        "ping": {
            "server": "fifteen"
        }
    }

e.t.c.

I tried implementing the SwiftyJSON array library, but didn't really understand how to append values to the json itself.

Is there a way to manage it?

Appreciate any insights!

5
  • 3
    What you have tried? show your code. Commented Sep 26, 2015 at 7:00
  • I just added the swift library file, I've seen some examples of for loops on swiftly github, but no more than that Commented Sep 26, 2015 at 7:06
  • That's not an array, it's a dictionary. JSON arrays are represented with square brackets [] Commented Sep 26, 2015 at 7:59
  • Yes you are absolutely correct. Thank you for clarification. How should I proceed with value appending? @vadian Commented Sep 26, 2015 at 12:09
  • to process the dictionaries in Swift create an Array, convert the JSON string to dictionary with SwiftyJSON, and append the dictionary to the array. Commented Sep 26, 2015 at 13:33

2 Answers 2

1

I hope this is what you are looking for. It took me some time to understand what you are asking for :)

// Initial Data - So called JSON Array
var dict1 = ["connection" : ["established": "yes"], "ping" : ["server" : "twelve"]]
var dict2 = ["connection" : ["established": "yes"], "ping" : ["server" : "thirteen"]]
var array = [dict1, dict2]

// Servers Array
var servers = ["fourteen", "fifteen"]

// First lets filter out dictionary where ping.server = thirteen
let predicate = NSPredicate(format: "ping.server = %@", "thirteen")

// Filtered dictionary
var targetDict = array.filter({
    predicate.evaluateWithObject($0)
})[0]

// Now lets loop on servers and modify filtered dictionary and add to parent array
for server in servers {
    targetDict["ping"]!["server"]! = server
    array.append(targetDict)
}

print("\(array)")
Sign up to request clarification or add additional context in comments.

2 Comments

works as a charm! thank you ! sorry for not being clear enough
No worries... Glad it helped!
0

Try this using SwiftyJSON (Not tested)

Json["ping"] as NSDictionary)["server"] as NSString = "Your Value"

1 Comment

not sure how it's supposed to work... once i insert the code, i'm getting "Expecting expression for size of array type" error. And what about the ")" symbol, should it be there?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.