-2

I received data from api hit that is base64 encoded. So i converted it into string and decoded it using extension method. After this again converted into data using utf8.

do{
     let jsonResult = String(data: data, encoding: .utf8)
     let jsonDecryptedString = jsonResult?.fromBase64()
     let jsonData = jsonDecryptedString?.data(using: .utf8)
  }catch let err{
            print(err)
  }

This is my Json String (jsonDecryptedString)

{
    'error_Code': '0',
    'error_msg' : 'Valid Request', 
    'Report_list' :
    [
       {
           “ReportID" : "LEGAL-01","ReportRdlName" : "Statement of Suit filed Accounts(Pending for Judgement)","ReportType" :           "1","ReportShortNameEnum" : "SF Pending STMT"
       },
       {
           "ReportID" : "LEGAL-02","ReportRdlName" : "Suit Filed Account during the Month","ReportType" : "1","ReportShortNameEnum" :               "SF During Month"}
       }
   ]
}

How to get "Report_list" from it using JSONSerialization

In jsonResult i received

"eydlcnJvcl9Db2RlJzogJzAnLCdlcnJvcl9tc2cnIDogJ1ZhbGlkIFJlcXVlc3QnLCAnY2FzZV9saXN0JyA6IFt7IkN1c3RvbWVyTmFtZSIgOiAiTVIgQUJISU5BViBTSEFSTUEiLCJjYXNlX2lkIiA6ICIiLCJBZHZJRCIgOiAibml0ZXNoMTIzIiwiU3VpdFR5cGUiIDogIjEiLCJjYXNlX25hbWUiIDogIlN1aXQgLyBBcHBsaWNhdGlvbiIsInNlc3Npb25fSWQiIDogIjQwOTQ5OTg4OSIsIkJyYW5jaENvZGUiIDogIjA0MDgifV19"

In jsonDecryptedString

"{\'error_Code\': \'0\',\'error_msg\' : \'Valid Request\', \'case_list\' : [{\"CustomerName\" : \"MR ABHINAV SHARMA\",\"case_id\" : \"\",\"AdvID\" : \"nitesh123\",\"SuitType\" : \"1\",\"case_name\" : \"Suit / Application\",\"session_Id\" : \"409499889\",\"BranchCode\" : \"0408\"}]}"

And Error is

The data couldn’t be read because it isn’t in the correct format.
Dilip Jangid
  • 754
  • 1
  • 10
  • 20

2 Answers2

0
try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any]
GregP
  • 1,584
  • 17
  • 16
  • it goes to catch block. i did this ->do { let json = try JSONSerialization.jsonObject(with: jsonData!, options: []) as? [String: Any] } catch let error as NSError { print("Failed to load: \(error.localizedDescription)") } – Dilip Jangid Apr 17 '17 at 14:12
  • Put a break point on each line in the do section and step through and see what the values are as you go along. Try not to cast as [String:Any] and see what you get also. – GregP Apr 17 '17 at 14:16
  • Please Check again. I have modified my post and also tried without [String:Any] – Dilip Jangid Apr 17 '17 at 14:31
0

Actually its problem with Json that is not in proper formate. I replace single quote with double

let jsonResult = String(data: data, encoding: .utf8)
let jsonDecodedString1 = jsonResult?.fromBase64()
let jsonDecodedString2 = jsonDecodedString1?.replacingOccurrences(of: "'", with:"\"")      

Now it works properly.

To print case_list

if let data = jsonDecodedString2?.data(using: String.Encoding.utf8) 
{
    let json = JSON(data: data)

    for item in json["case_list"]
    {
        print(item)
    }            
}

Here is the Output

 ("0", 
 {
    "CustomerName" : "MR ABHINAV SHARMA",
    "SuitType" : "1",
    "AdvID" : "nitesh123",
    "case_name" : "Suit \/ Application",
    "case_id" : "",
    "BranchCode" : "0408",
    "session_Id" : "738007867"
})

Below is correct Json formate

let json = "{\"error_Code\": \"0\",\"error_msg\" : \"Valid Request\", \"case_list\" : [{\"CustomerName\" : \"MR ABHINAV SHARMA\",\"case_id\" : \"\",\"AdvID\" : \"nitesh123\",\"SuitType\" : \"1\",\"case_name\" : \"Suit / Application\",\"session_Id\" : \"923137599\",\"BranchCode\" : \"0408\"}]}"
Dilip Jangid
  • 754
  • 1
  • 10
  • 20