-1

I'm creating a drawing application in Swift that can be redrawn on canvas for HTML. Just a prototype. What I need is to get this data in JSON format to my database. I organized everything in arrays and dictionaries.

How can I generate something like this:

 {
  "data": [

  {

     "width": 150,

     "height": 200,

     "x": 45,

     "y": 65,

     "id": "line4"

  },

  {

     "width": 150,

     "height": 200,

     "x": 45,

     "y": 65,

     "id": "Shape1"

  },

  {

     "width": 150,

     "height": 200,

     "x": 45,

     "y": 65,

     "id": "line3"

  },

  {

     "width": 350,

     "height": 400,

     "x": 45,

     "y": 65,

     "id": "line2"

  }

  ]

  }

Using Arrays and Dicionaries (and / or Structs) and then turn into Json.

I tried to create a one Dictionary within an array within another Dictionary. The data is dynamic, that is the big problem.

help will be appreciated

Maulik shah
  • 1,664
  • 1
  • 19
  • 45
James
  • 1,167
  • 1
  • 13
  • 37

3 Answers3

1
var arr: Array<Any> = []
var dict1: Dictionary<String,Any> = [:]
var dict: Dictionary<String,Any> = [:]

arr = ["text",1,true]
dict1["arr"] = arr
dict["dict1"] = dict1

// 

var dict2: Dictionary<String,Any> = [:]
dict["dict2"] = dict2

print(dict) // ["dict2": [:], "dict1": ["arr": ["text", 1, true]]]

withUnsafeMutablePointer(&dict["dict2"]) { (pdict2) -> Void in
    var pd = UnsafeMutablePointer<Dictionary<String,Any>>(pdict2)
    pd.memory.updateValue(arr, forKey: "arr2")
}

print(dict) // ["dict2": ["arr2": ["text", 1, true]], "dict1": ["arr": ["text", 1, true]]]

you can do it. i don't recommend you this approach. you are better to redesign you idea

user3441734
  • 16,722
  • 2
  • 40
  • 59
0

Here it says how to do it in Objective-C. You can port this solution to Swift.

Generate JSON string from NSDictionary in iOS

Community
  • 1
  • 1
BR41N-FCK
  • 766
  • 6
  • 17
  • The problem is Insert Dictionary in Array, inside another Dictionary. But tanks for your answer. – James Nov 27 '15 at 07:53
  • Do you need swift code? `var arr: [[key:value]]; // your array` `var dict1:[key:value] // your dictionary` `arr.append(dict1) // insert dictionary in the array` `var dict2:[key : [[key:value]]] //this dictionary will store your array` `dict2[key] = arr` – BR41N-FCK Nov 27 '15 at 08:01
0

Not sure if that was your question but to create static structure all you have to do is to replace your curly braces with square brackets.

let data = [ "data": [
    [

        "width": 150,

        "height": 200,

        "x": 45,

        "y": 65,

        "id": "line4"

    ],
    [

        "width": 150,

        "height": 200,

        "x": 45,

        "y": 65,

        "id": "line1"

    ]
]
]

And to serialize it to JSON data:

let jsonData = try! NSJSONSerialization.dataWithJSONObject(data, options: NSJSONWritingOptions.PrettyPrinted)

And to deserialize back to Swift objects:

let data2 = try! NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.AllowFragments)

You might also want to create JSON string from the data:

let jsonString = String(data: jsonData, encoding: NSUTF8StringEncoding)

Or get data from JSON string:

let dataFromString = jsonString?.dataUsingEncoding(NSUTF8StringEncoding)

Of course you can then convert it to Swift objects using NSJSONSerialization.

Adam
  • 26,549
  • 8
  • 62
  • 79
  • Good answer, but still not exactly what I need. The 'data' must be supplied dynamically (for a function or something). – James Nov 27 '15 at 08:02
  • What do you mean by "supplied dynamically"? To get exact answers you should specify exactly what you need. – Adam Nov 27 '15 at 08:04