0

I load a json array with this format

[{
    id = 1;
    name = “test1”;
}, {
    id = 2;
    name = “test2”;
}, {
    id = 3;
    name = “test#”;
}, {
    id = 4;
    name = “test$”;
}]

And I have a class called MyClass(id: int, name: String). For each element in the json I want to create an instance of MyClass. objects containt an array of the json element. Would it be possible to do something like

for object in objects{
MyClass(object)
}

or how shall I parse the json?

Verv
  • 2,385
  • 2
  • 23
  • 37
user567
  • 3,712
  • 9
  • 47
  • 80
  • That is *not JSON.* It looks like the description of an `(NS)Array` – Martin R Aug 25 '17 at 14:24
  • @MartinR Yes, thats the JSON parsed to NSArray – user567 Aug 25 '17 at 14:24
  • 1
    So you already have parsed the array and can iterate over it. What is your question? – Martin R Aug 25 '17 at 14:25
  • How to create an instance of MyClass from the objects contained in the array – user567 Aug 25 '17 at 14:26
  • @user567 Before posting such a novice question, first please learn swift a little bit. [A Swift Tour](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html#//apple_ref/doc/uid/TP40014097-CH2-ID1) might be very helpful. And answer to you question, please take a look on the link [MyClass Initialization Example](https://swift.sandbox.bluemix.net/#/repl/59a04ce4f2733d2f5ebc6359) – Mehedi Hasan Aug 25 '17 at 16:38
  • @MehediHasan Please read at least the question before commenting. This is exactly what I don't want to do. What if my json hat 30 attributes? The solution I was looking for is is to use self.setValuesForKeysWithDictionary(JSONDictionary) at the initializer. – user567 Aug 25 '17 at 17:40

0 Answers0