2016-08-16 38 views
0

代碼一個JSON解析錯誤,下面是詳細信息:

func callAddWithPOST(Name mname:String, PhoneNo mphone:String, Email memail:String, Comment mcomments:String){ 
var names = [String]() 
let login = ["countryId":"1"] 
print("Your Result is : = \(login)") 
let url = NSURL(string: "http://photokeeper.mgtcloud.co.uk/commonwebservice.asmx/getStateList")! 
let session = NSURLSession.sharedSession() 
let request = NSMutableURLRequest(URL: url) 
do { 
let auth = try NSJSONSerialization.dataWithJSONObject(login, options: .PrettyPrinted) 
request.setValue("application/json", forHTTPHeaderField: "Content-Type") 
request.HTTPMethod = "POST" 
request.HTTPBody = auth 
    let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in 
    let badJsonString = "This really isn't valid JSON at all" 
    let badJsonData = badJsonString.dataUsingEncoding(NSUTF8StringEncoding)! 
    do { 
     let parsed = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) 
     print(parsed) 
     let otherParsed = try NSJSONSerialization.JSONObjectWithData(badJsonData, options: NSJSONReadingOptions.AllowFragments) 
    } 
    catch let error as NSError { 
     print("A JSON parsing error occurred, here are the details:\n \(error)") 
    } 
print("Done.") 
}) 
task.resume() 
} catch { 
print("Error") 
}} 

OUTPUT發生

{ 
    d = "{\"result\":[{\"stateId\":3871,\"stateName\":\"Aberdeenshire\"},{\"stateId\":3872,\"stateName\":\"Anglesey/Sir Fon\"},{\"stateId\":3873,\"stateName\":\"Angus\"},{\"stateId\":3874,\"stateName\":\"Antrim\"},{\"stateId\":3875,\"stateName\":\"Argyll And Bute\"},{\"stateId\":3876,\"stateName\":\"Armagh\"},{\"stateId\":3877,\"stateName\":\"Ayrshire\"},{\"stateId\":3878,\"stateName\":\"Bedfordshire\"},{\"stateId\":3879,\"stateName\":\"Berkshire\"},{\"stateId\":3880,\"stateName\":\"Blaenau Gwent/Blaenau Gwent\"},{\"stateId\":3881,\"stateName\":\"Bristol\"},{\"stateId\":3882,\"stateName\":\"Buckinghamshire\"},{\"stateId\":3883,\"stateName\":\"Caerphilly/Caerffili\"},{\"stateId\":3884,\"stateName\":\"Cambridgeshire\"},{\"stateId\":3885,\"stateName\":\"Cardiff/Caerdydd\"},{\"stateId\":3886,\"stateName\":\"Cardiganshire/Ceredigion\"},{\"stateId\":3888,\"stateName\":\"Carmarthenshire/Sir Gaerfyrddin\"},{\"stateId\":3890,\"stateName\":\"Cheshire\"},{\"stateId\":3891,\"stateName\":\"Clackmannanshire\"},{\"stateId\":3893,\"stateName\":\"Conwy/Conwy\"},{\"stateId\":3895,\"stateName\":\"County Durham\"},{\"stateId\":3896,\"stateName\":\"Cumbria\"},{\"stateId\":3897,\"stateName\":\"Denbighshire/Sir Ddinbych\"},{\"stateId\":3898,\"stateName\":\"Derbyshire\"},{\"stateId\":3899,\"stateName\":\"Devon\"},{\"stateId\":3901,\"stateName\":\"Dorset\"},{\"stateId\":3902,\"stateName\":\"Down\"},{\"stateId\":3904,\"stateName\":\"Dumfries And Galloway\"},{\"stateId\":3905,\"stateName\":\"Dunbartonshire\"},{\"stateId\":3906,\"stateName\":\"Dundee\"},{\"stateId\":3907,\"stateName\":\"Durham/North Yorkshire\"},{\"stateId\":3908,\"stateName\":\"East Lothian\"},{\"stateId\":3909,\"stateName\":\"East Sussex\"},{\"stateId\":3910,\"stateName\":\"East Yorkshire\"},{\"stateId\":3911,\"stateName\":\"Edinburgh\"},{\"stateId\":3912,\"stateName\":\"Essex\"},{\"stateId\":3913,\"stateName\":\"Fermanagh\"},{\"stateId\":3914,\"stateName\":\"Fife\"},{\"stateId\":3915,\"stateName\":\"Flintshire/Sir Fflint\"},{\"stateId\":3917,\"stateName\":\"Glamorgan/Morgannwg\"},{\"stateId\":3918,\"stateName\":\"Glasgow\"},{\"stateId\":3919,\"stateName\":\"Gloucestershire\"},{\"stateId\":3920,\"stateName\":\"Gwynedd/Gwynedd\"},{\"stateId\":3921,\"stateName\":\"Hampshire\"},{\"stateId\":3922,\"stateName\":\"Herefordshire\"},{\"stateId\":3923,\"stateName\":\"Hertfordshire\"},{\"stateId\":3924,\"stateName\":\"Highland\"},{\"stateId\":3925,\"stateName\":\"Kent\"},{\"stateId\":3929,\"stateName\":\"Lanarkshire\"},{\"stateId\":3930,\"stateName\":\"Lancashire\"},{\"stateId\":3932,\"stateName\":\"Leicestershire\"},{\"stateId\":3935,\"stateName\":\"Lincolnshire\"},{\"stateId\":3936,\"stateName\":\"London\"},{\"stateId\":3937,\"stateName\":\"Londonderry\"},{\"stateId\":3940,\"stateName\":\"Manchester\"},{\"stateId\":3943,\"stateName\":\"Merthyr Tydfil/Merthyr Tydfil\"},{\"stateId\":3944,\"stateName\":\"Midlothian\"},{\"stateId\":3946,\"stateName\":\"Monmouthshire/Sir Fynwy\"},{\"stateId\":3947,\"stateName\":\"Moray\"},{\"stateId\":3948,\"stateName\":\"Neath Port Talbot\"},{\"stateId\":3949,\"stateName\":\"Newport\"},{\"stateId\":3950,\"stateName\":\"Norfolk\"},{\"stateId\":3951,\"stateName\":\"Northamptonshire\"},{\"stateId\":3952,\"stateName\":\"Northumberland\"},{\"stateId\":3953,\"stateName\":\"Nottinghamshire\"},{\"stateId\":3955,\"stateName\":\"Orkney\"},{\"stateId\":3956,\"stateName\":\"Oxfordshire\"},{\"stateId\":3957,\"stateName\":\"Pembrokeshire/Sir Benfro\"},{\"stateId\":3958,\"stateName\":\"Perth And Kinross\"},{\"stateId\":3959,\"stateName\":\"Powys/Powys\"},{\"stateId\":3960,\"stateName\":\"Renfrewshire\"},{\"stateId\":3962,\"stateName\":\"Rutland\"},{\"stateId\":3963,\"stateName\":\"Scottish Borders\"},{\"stateId\":3964,\"stateName\":\"Shetland Isles\"},{\"stateId\":3965,\"stateName\":\"Shropshire\"},{\"stateId\":3967,\"stateName\":\"Somerset\"},{\"stateId\":3968,\"stateName\":\"South Yorkshire\"},{\"stateId\":3969,\"stateName\":\"Staffordshire\"},{\"stateId\":3970,\"stateName\":\"Stirling\"},{\"stateId\":3971,\"stateName\":\"Suffolk\"},{\"stateId\":3972,\"stateName\":\"Surrey\"},{\"stateId\":3973,\"stateName\":\"Swansea\"},{\"stateId\":3975,\"stateName\":\"Torfaen\"},{\"stateId\":3976,\"stateName\":\"Tyrone\"},{\"stateId\":3977,\"stateName\":\"Warwickshire\"},{\"stateId\":3979,\"stateName\":\"West Lothian\"},{\"stateId\":3980,\"stateName\":\"West Midlands\"},{\"stateId\":3981,\"stateName\":\"West Sussex\"},{\"stateId\":3982,\"stateName\":\"West Yorkshire\"},{\"stateId\":3983,\"stateName\":\"Western Isles\"},{\"stateId\":3987,\"stateName\":\"Wiltshire\"},{\"stateId\":3988,\"stateName\":\"Worcestershire\"},{\"stateId\":3989,\"stateName\":\"Wrexham\"}],\"status\":\"success\"}"; 
} 

一個JSON解析錯誤,下面是詳細信息: 錯誤域= NSCocoaErrorDomain代碼= 3840「字符0周圍的值無效。」 UserInfo = {NSDebugDescription =字符0周圍的值無效。} 完成。

我得到錯誤 - Invalid value around character 0.我想以正確的格式獲得所需的鍵和值的數據,請有人幫我解決這個問題。

+0

可以粘貼RESTClient實現響應? – Bhupesh

+0

你可以檢查你的json數據是否有效[HERE](http://pro.jsonlint.com/).. – vaibhav

回答

0

試試這個,

if let result:String = parsed["d"]! { 
    let result = convertStringToDictionary(text: result) 
    print("Converted result = \(result)") 
} 

//SWIFT 3 
func convertStringToDictionary(text: String) -> [String:AnyObject]? { 
    if let data = text.data(using: String.Encoding.utf8) { 
     do { 
      return try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject] 
     } catch let error as NSError { 
      print(error) 
     } 
    } 
    return nil 
} 

//SWIFT 2 
func convertStringToDictionary(text: String) -> [String:AnyObject]? { 
    if let data = text.dataUsingEncoding(NSUTF8StringEncoding) { 
     do { 
      return try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String:AnyObject] 
     } catch let error as NSError { 
      print(error) 
     } 
    } 
    return nil 
} 
0

您的JSON響應已損壞。使用JSONLint來驗證它。

轉換從

{\"result\":[{\"stateId\":3871,\"stateName\":\"Aberdeenshire\"} 

{ 
    "result": { 
     "stateId": 3871, 
     "stateName": "Aberdeenshire" 
    } 
} 

注意去除反斜槓。

0

看起來你在你的回覆中得到了String,所以試試這樣。

do { 
    let parsed = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! [String: AnyObject] 
    let responseStr = parsed["d"] as! String 
    let correctData = responseStr.dataUsingEncoding(NSUTF8StringEncoding) 
    let responseDic = try NSJSONSerialization.JSONObjectWithData(correctData, options: NSJSONReadingOptions.AllowFragments) as! [String: AnyObject] 
    print(responseDic) 
} 
catch let error as NSError { 
    print("A JSON parsing error occurred, here are the details:\n \(error)") 
} 
+0

變量在自己的初始值中使用 - >變量'correctData' 連續的語句在一行必須用':'分隔 - > catch讓錯誤成爲NSError { –

+0

在你的代碼塊中出現以上錯誤 –

+0

對不起,添加了'{'括號後刪除它,檢查我編輯的答案。 –