0

I've try to search around and can't find anything to solve the problem. Here is the json reply like this and i want to change to [NSDictionary]:

let reply = "[{"qty":"3","price":"75000"},{"qty":"4","price":"75000"},    
{"qty":"1","price":"60000"},{"qty":"2","price":"60000"},{"qty":"6","price":"80000"}]"

let array = [{"qty":"3","price":"75000"},{"qty":"4","price":"75000"},
{"qty":"1","price":"60000"},{"qty":"2","price":"60000"},{"qty":"6","price":"80000"}]

Can someone please help me with examples. Thank you!

2
  • Use this library: github.com/SwiftyJSON/SwiftyJSON Commented Jan 31, 2017 at 9:36
  • Do not – never do – use NSArray / NSDictionary in Swift in conjunction with JSON. Commented Jan 31, 2017 at 10:08

4 Answers 4

2

From your statement:

i want to change to [NSDictionary]

I assume it as:

Array<Dictionary<String, Any>>  // using Swift v3

Although there is a more handy solution is to use SwiftyJSON library, but a non-third party solution could be:

let reply = "[{\"qty\":\"3\",\"price\":\"75000\"},{\"qty\":\"4\",\"price\":\"75000\"},{\"qty\":\"1\",\"price\":\"60000\"},{\"qty\":\"2\",\"price\":\"60000\"},{\"qty\":\"6\",\"price\":\"80000\"}]"

do {
       let data  = try JSONSerialization.jsonObject(with: reply.data(using: .utf8)!, options: .allowFragments) as? Array<Dictionary<String, Any>>

        let firstElement: Dictionary<String, Any> = data!.first!
        print("First dictionary element: \(firstElement)")
        print("Quantity from first dictionary element: \(firstElement["qty"] as! String)")
 }
 catch{
        print ("Handle error")
 }

Output:

First dictionary element: ["qty": 3, "price": 75000]
Quantity from first dictionary element: 3

NOTE:

I didn't handle the nil checks and also i converted string back to data to get json object, if you already have data no need to call reply.data(using: .utf8)! instead pass your data.

As per the above comment:

You can also equate:

Array<Dictionary<String, Any>> = [[String: Any]]
Dictionary<String, Any>        =  [String: Any]
Sign up to request clarification or add additional context in comments.

Comments

1

Try This:

 let jsonText = "[{\"qty\":\"3\",\"price\":\"75000\"},{\"qty\":\"4\",\"price\":\"75000\"},{\"qty\":\"1\",\"price\":\"60000\"},{\"qty\":\"2\",\"price\":\"60000\"},{\"qty\":\"6\",\"price\":\"80000\"}]"
        var myData:NSArray?

        if let data = jsonText.data(using: String.Encoding.utf8) {

            do {
                myData = try JSONSerialization.jsonObject(with: data, options: []) as? NSArray

                if let resultdata = myData
                {
                    print(resultdata)
                }
            } catch let error as NSError {
                print(error)
            }
        }

Comments

0

reply is already an array of Dictionary. What are you actually asking? If you need to convert a string representation of a json reply into a manageable structure, then as @Imad suggests, you should use one of the many JSON libraries that are available, and SwiftyJSON is one of the best.

Once you get your reply, all you need is something like this

let json = JSON(data: dataFromNetworking)

Also, if you are including code, make sure it is real code - what you have shown will generate errors, but this will not.

let reply = [["qty":"3","price":"75000"],
             ["qty":"4","price":"75000"],
             ["qty":"1","price":"60000"],
             ["qty":"2","price":"60000"],
             ["qty":"6","price":"80000"]]

Comments

0
var json_Parameters = NSArray() 

let reply = "[{\"qty\":\"3\",\"price\":\"75000\"},{\"qty\":\"4\",\"price\":\"75000\"},{\"qty\":\"1\",\"price\":\"60000\"},{\"qty\":\"2\",\"price\":\"60000\"},{\"qty\":\"6\",\"price\":\"80000\"}]"

Convert the Json string to json data and use the below code to type caste as an Array ()

if let data = reply.data(using: String.Encoding.utf8) {
do {
    let datwa = try JSONSerialization.jsonObject(with: data, options: []) as! NSArray
    json_Parameters = datwa
}

}

3 Comments

NSArray when dealing with JSON in Swift seems to be pretty easy but it's very very bad. You throw away the type information which is essential for the compiler to create efficient code. You are strongly discouraged from using Foundation collection types in Swift anyway.
@Vadian I have done only what "SawSoeMoeNyunt" asked.
@BraneDullet And sometimes only doing what OP wants is not the best answer. :)

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.