1

I have :-

var myArray:Array = array<JSON>

I want :-

var myJSON:JSON = myArray as? JSON

but i get a warning:- Cast from 'Array' to unrelated type 'JSON' always fails

Can anyone help me how to convert an Array object to a JSON object?

A little more insight what i am trying to do:- (This might not be needed to answer my above question but still...) Note:- I am using objectMapper for mapping swiftyJSONObjects.

var monthArray = Array<JSON>
var allEventsInDayArray:Array<JSON> = [] // an empty array
        for (_,dayObj) in monthArray
        {
            guard let dayEventsArray = dayObj["events"].array else { break   }

        allEventsInDayArray.appendContentsOf(dayEventsArray)
    }

    print(allEventsInDayArray)
    var allEventsJSON:JSON? = JSON(allEventsInDayArray)
    var EventModelArray:Array<EventModel>? = Mapper<EventModel>().mapArray(allEventsJSON?.object.allValues)
6
  • You can't make an orange into an apple just with downcasting. Please explain what you want to achieve - maybe there's a better way to do it... ;) Commented Apr 12, 2016 at 14:38
  • i agree @Eric D. i Just want an apple can you tell me how can i get it. I dont have an orange but an array of apples ;) Forget about downcasting, i just want to convert an array of Swifty JSON Objects to a SwiftyJSON object. Commented Apr 12, 2016 at 14:46
  • Hehe, thanks for answering. :) So indeed we don't care about downcasting here. Unfortunately I don't see how to do this. I will answer if I find out. // May I ask why you want this, though? Maybe you have an X/Y problem that could be resolved in a different way, that's also what I meant earlier. Commented Apr 12, 2016 at 14:49
  • Sure @EricD. Please see my edited question above Commented Apr 12, 2016 at 14:56
  • 2
    This's the code it's calling: init(_ jsonArray:[JSON]) Commented Apr 12, 2016 at 15:12

1 Answer 1

2

If I understood your question and you want to create a SwiftyJSON object from an array you should do like this:

var myJSON:JSON = JSON(myArray)
Sign up to request clarification or add additional context in comments.

Comments

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.