0

I am having an api in which after parsing json the response gets as below mentioned and after that I had removed slashes also but unable to save it into dictionary format can anyone help me how to implement this ?

Here is my code

func productListSortDownloadJsonWithURL() {
        let url = URL(string: sortIndicesURL)!
        var request = URLRequest(url: url as URL)
        request.httpMethod = "GET"
        request.addValue("Basic Z29tZXRvb2FkOm55UWREZ1ZYMWdYU1FkMmhHWEpN", forHTTPHeaderField: "Authorization")
        let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
            if error != nil { print(error!); return }
            do {
                if let jsonObj = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? String {
                    print(jsonObj)
                    self.secondIndices = jsonObj.replacingOccurrences(of:"\"", with: "")
                    DispatchQueue.main.async {

                    }
                }
            } catch {
                print(error)

            }
        }
        task.resume()
    }

here is my json data

"{\"1\":{\"attribute\":\"price_index\",\"sort\":\"desc\",\"sortLabel\":\"Highest price\"},\"0\":{\"attribute\":\"price_index\",\"sort\":\"asc\",\"sortLabel\":\"Lowest price\"},\"2\":{\"attribute\":\"rating_summary\",\"sort\":\"desc\",\"sortLabel\":\"Top Rated\"}}"

rmaddy
  • 314,917
  • 42
  • 532
  • 579
User
  • 73
  • 1
  • 8

2 Answers2

0

swift 3.0

extension Data
{
func dataToJSON() -> Any? {
    do {
        return try JSONSerialization.jsonObject(with: self, options: .mutableContainers)
    } catch let myJSONError {
        print(myJSONError)
    }
    return nil
}
}

Usage

if let dictResponse = data?.dataToJSON() as? NSDictionary {
 print(dictResponse)
} 
Pratik Prajapati
  • 1,137
  • 1
  • 13
  • 26
-1

Try this,

let yourDictionary = NSKeyedUnarchiver.unarchiveObject(with: data!) as? Dictionary

nikBhosale
  • 531
  • 1
  • 5
  • 27
  • after parsing I need to pass the data into this ? – User Feb 20 '18 at 04:43
  • yes, you already have data, just pass it into this. It should work. – nikBhosale Feb 20 '18 at 04:44
  • I tried to pass but I got error as shown here https://i.stack.imgur.com/JjaT9.png – User Feb 20 '18 at 04:47
  • You are doing it wrong, let task = URLSession.shared.dataTask(with: request) { (data, response, error) in if error != nil { print(error!); return } do { let yourDictionary: Dictionary? = NSKeyedUnarchiver.unarchiveObject(with: data) as! [String : Any] print(yourDictionary) DispatchQueue.main.async { } } catch { print(error) } } – nikBhosale Feb 20 '18 at 04:49
  • then how to implement it ? @nikBhosale – User Feb 20 '18 at 04:50
  • Don't use 'jsonObj', use 'data' as argument directly. – nikBhosale Feb 20 '18 at 04:52
  • it again crashed at the line what u said to change https://i.stack.imgur.com/QW0Oc.png – User Feb 20 '18 at 04:59
  • Comment everything and try only, let yourDictionary = NSKeyedUnarchiver.unarchiveObject(with: data!) as? Dictionary print(yourDictionary) – nikBhosale Feb 20 '18 at 05:04