为什么我的 json 格式化字符串没有通过使用 swift 中的 swiftyjson 库转换为 json 数组 3

Why is my json formatted string not getting converted into json array by using swiftyjson library in swift 3

我正在尝试使用 ios 中的 swift json 将作为字符串响应从服务器获取的以下字符串转换为 json 数组。但它没有被转换成字符串。

这是我的字符串:

[{\"Sno\":\"1\",\"Grivence_Code\":\"5511\",\"CaseType\":\"ACP\",\"MarkedTo\":\"DGSE\",\"Applied_Date\":\"09-Jun-2016\",\"Description\":\"hahahha\",\"CurrentStatus\":\"Disposed Off\",\"Action_Code\":\"6290\",\"Action_TakenBy\":\"DGSE\",\"Action_Date\":\"28-Jun-2016\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740688052\"},{\"Sno\":\"2\",\"Grivence_Code\":\"5516\",\"CaseType\":\"ACP\",\"MarkedTo\":\"DGSE\",\"Applied_Date\":\"09-Jun-2016\",\"Description\":\"\n\n\n\n\n\n\",\"CurrentStatus\":\"Disposed Off\",\"Action_Code\":\"6290\",\"Action_TakenBy\":\"DGSE\",\"Action_Date\":\"28-Jun-2016\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740674400\"},{\"Sno\":\"3\",\"Grivence_Code\":\"5522\",\"CaseType\":\"ACP\",\"MarkedTo\":\"DGSE\",\"Applied_Date\":\"12-Jun-2016\",\"Description\":\"testing testing testing testing testing testing \",\"CurrentStatus\":\"Disposed Off\",\"Action_Code\":\"6290\",\"Action_TakenBy\":\"DGSE\",\"Action_Date\":\"28-Jun-2016\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740663938\"},{\"Sno\":\"4\",\"Grivence_Code\":\"5529\",\"CaseType\":\"ACP\",\"MarkedTo\":\"CEO(Nabha)\",\"Applied_Date\":\"13-Jun-2016\",\"Description\":\"testing. \",\"CurrentStatus\":\"Pending\",\"Action_Code\":\"\",\"Action_TakenBy\":\"CEO(Nabha)\",\"Action_Date\":\"\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740659500\"},{\"Sno\":\"5\",\"Grivence_Code\":\"5530\",\"CaseType\":\"ACP\",\"MarkedTo\":\"DGSE\",\"Applied_Date\":\"13-Jun-2016\",\"Description\":\"bbbnnnnnnbbbvvvvvvbbbbvbbbb\",\"CurrentStatus\":\"Disposed Off\",\"Action_Code\":\"6290\",\"Action_TakenBy\":\"DGSE\",\"Action_Date\":\"28-Jun-2016\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740647081\"},{\"Sno\":\"6\",\"Grivence_Code\":\"5531\",\"CaseType\":\"ACP\",\"MarkedTo\":\"DGSE\",\"Applied_Date\":\"13-Jun-2016\",\"Description\":\" vvbhhhhhhhhhhgvg. vvbhhhhhhhhhhgvg. \",\"CurrentStatus\":\"Disposed Off\",\"Action_Code\":\"6290\",\"Action_TakenBy\":\"DGSE\",\"Action_Date\":\"28-Jun-2016\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740634790\"},{\"Sno\":\"7\",\"Grivence_Code\":\"5532\",\"CaseType\":\"ACP\",\"MarkedTo\":\"DGSE\",\"Applied_Date\":\"13-Jun-2016\",\"Description\":\"I m in prakshvtadavhjhgggggggggggghggghhhg\",\"CurrentStatus\":\"Disposed Off\",\"Action_Code\":\"6290\",\"Action_TakenBy\":\"DGSE\",\"Action_Date\":\"28-Jun-2016\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740678909\"},{\"Sno\":\"8\",\"Grivence_Code\":\"5728\",\"CaseType\":\"Increment related\",\"MarkedTo\":\"CEO(Jalandhar)\",\"Applied_Date\":\"01-Aug-2016\",\"Description\":\"rigorous testing is the most mandatory step towards developing robust software \",\"CurrentStatus\":\"Disposed Off\",\"Action_Code\":\"6718\",\"Action_TakenBy\":\"CEO(Jalandhar)\",\"Action_Date\":\"12-Aug-2016\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740611379\"},{\"Sno\":\"9\",\"Grivence_Code\":\"5884\",\"CaseType\":\"ACP\",\"MarkedTo\":\"DGSE\",\"Applied_Date\":\"10-Sep-2016\",\"Description\":\"hellovvnnbvccccdccxxxccccccvv\",\"CurrentStatus\":\"Disposed Off\",\"Action_Code\":\"6290\",\"Action_TakenBy\":\"DGSE\",\"Action_Date\":\"12-Sep-2016\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740697305\"},{\"Sno\":\"10\",\"Grivence_Code\":\"5885\",\"CaseType\":\"Probation Period related\",\"MarkedTo\":\"Director (SCERT)\",\"Applied_Date\":\"10-Sep-2016\",\"Description\":\"vgccgggcgggggvbvvhbbvvbbbbbbbb\",\"CurrentStatus\":\"Pending\",\"Action_Code\":\"\",\"Action_TakenBy\":\"Director (SCERT)\",\"Action_Date\":\"\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740686213\"},{\"Sno\":\"11\",\"Grivence_Code\":\"6176\",\"CaseType\":\"ACR\",\"MarkedTo\":\"DEO(EE)-BATHINDA\",\"Applied_Date\":\"05-Jan-2017\",\"Description\":\"Addedczxvxvxvdzvdgdsdgdsxgdgdsgdfgdsgdss\",\"CurrentStatus\":\"Pending\",\"Action_Code\":\"\",\"Action_TakenBy\":\"DEO(EE)-BATHINDA\",\"Action_Date\":\"\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740618188\"}]

这是我的代码:

if let dataFromString = string.data(using: .utf8, allowLossyConversion: false) {

        let json = JSON(data: dataFromString)                                           
        var i=0

        for i in 0..<json.count {    

        }
}

它总是给出 json 计数 0。请帮助。

let swiftyJsonVar = "[{\"Sno\":\"1\",\"Grivence_Code\":\"5511\",\"CaseType\":\"ACP\",\"MarkedTo\":\"DGSE\",\"Applied_Date\":\"09-Jun-2016\",\"Description\":\"hahahha\",\"CurrentStatus\":\"Disposed Off\",\"Action_Code\":\"6290\",\"Action_TakenBy\":\"DGSE\",\"Action_Date\":\"28-Jun-2016\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740688052\"},{\"Sno\":\"2\",\"Grivence_Code\":\"5516\",\"CaseType\":\"ACP\",\"MarkedTo\":\"DGSE\",\"Applied_Date\":\"09-Jun-2016\",\"Description\":\"\",\"CurrentStatus\":\"Disposed Off\",\"Action_Code\":\"6290\",\"Action_TakenBy\":\"DGSE\",\"Action_Date\":\"28-Jun-2016\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740674400\"},{\"Sno\":\"3\",\"Grivence_Code\":\"5522\",\"CaseType\":\"ACP\",\"MarkedTo\":\"DGSE\",\"Applied_Date\":\"12-Jun-2016\",\"Description\":\"testing testing testing testing testing testing \",\"CurrentStatus\":\"Disposed Off\",\"Action_Code\":\"6290\",\"Action_TakenBy\":\"DGSE\",\"Action_Date\":\"28-Jun-2016\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740663938\"},{\"Sno\":\"4\",\"Grivence_Code\":\"5529\",\"CaseType\":\"ACP\",\"MarkedTo\":\"CEO(Nabha)\",\"Applied_Date\":\"13-Jun-2016\",\"Description\":\"testing. \",\"CurrentStatus\":\"Pending\",\"Action_Code\":\"\",\"Action_TakenBy\":\"CEO(Nabha)\",\"Action_Date\":\"\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740659500\"},{\"Sno\":\"5\",\"Grivence_Code\":\"5530\",\"CaseType\":\"ACP\",\"MarkedTo\":\"DGSE\",\"Applied_Date\":\"13-Jun-2016\",\"Description\":\"bbbnnnnnnbbbvvvvvvbbbbvbbbb\",\"CurrentStatus\":\"Disposed Off\",\"Action_Code\":\"6290\",\"Action_TakenBy\":\"DGSE\",\"Action_Date\":\"28-Jun-2016\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740647081\"},{\"Sno\":\"6\",\"Grivence_Code\":\"5531\",\"CaseType\":\"ACP\",\"MarkedTo\":\"DGSE\",\"Applied_Date\":\"13-Jun-2016\",\"Description\":\" vvbhhhhhhhhhhgvg. vvbhhhhhhhhhhgvg. \",\"CurrentStatus\":\"Disposed Off\",\"Action_Code\":\"6290\",\"Action_TakenBy\":\"DGSE\",\"Action_Date\":\"28-Jun-2016\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740634790\"},{\"Sno\":\"7\",\"Grivence_Code\":\"5532\",\"CaseType\":\"ACP\",\"MarkedTo\":\"DGSE\",\"Applied_Date\":\"13-Jun-2016\",\"Description\":\"I m in prakshvtadavhjhgggggggggggghggghhhg\",\"CurrentStatus\":\"Disposed Off\",\"Action_Code\":\"6290\",\"Action_TakenBy\":\"DGSE\",\"Action_Date\":\"28-Jun-2016\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740678909\"},{\"Sno\":\"8\",\"Grivence_Code\":\"5728\",\"CaseType\":\"Increment related\",\"MarkedTo\":\"CEO(Jalandhar)\",\"Applied_Date\":\"01-Aug-2016\",\"Description\":\"rigorous testing is the most mandatory step towards developing robust software \",\"CurrentStatus\":\"Disposed Off\",\"Action_Code\":\"6718\",\"Action_TakenBy\":\"CEO(Jalandhar)\",\"Action_Date\":\"12-Aug-2016\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740611379\"},{\"Sno\":\"9\",\"Grivence_Code\":\"5884\",\"CaseType\":\"ACP\",\"MarkedTo\":\"DGSE\",\"Applied_Date\":\"10-Sep-2016\",\"Description\":\"hellovvnnbvccccdccxxxccccccvv\",\"CurrentStatus\":\"Disposed Off\",\"Action_Code\":\"6290\",\"Action_TakenBy\":\"DGSE\",\"Action_Date\":\"12-Sep-2016\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740697305\"},{\"Sno\":\"10\",\"Grivence_Code\":\"5885\",\"CaseType\":\"Probation Period related\",\"MarkedTo\":\"Director (SCERT)\",\"Applied_Date\":\"10-Sep-2016\",\"Description\":\"vgccgggcgggggvbvvhbbvvbbbbbbbb\",\"CurrentStatus\":\"Pending\",\"Action_Code\":\"\",\"Action_TakenBy\":\"Director (SCERT)\",\"Action_Date\":\"\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740686213\"},{\"Sno\":\"11\",\"Grivence_Code\":\"6176\",\"CaseType\":\"ACR\",\"MarkedTo\":\"DEO(EE)-BATHINDA\",\"Applied_Date\":\"05-Jan-2017\",\"Description\":\"Addedczxvxvxvdzvdgdsdgdsxgdgdsgdfgdsgdss\",\"CurrentStatus\":\"Pending\",\"Action_Code\":\"\",\"Action_TakenBy\":\"DEO(EE)-BATHINDA\",\"Action_Date\":\"\",\"Remarks\":\"\",\"FacultyInfo_Code\":\"107406\",\"GrievanceNumber\":\"10740618188\"}]"
let finalJSON = JSON.parse(swiftyJsonVar)

print(finalJSON.arrayObject);
// you can use any of this to get your desired object
//print(finalJSON.arrayValue);
//print(finalJSON.array);

[

我终于找到了解决我自己问题的方法。 json 从字符串转换的问题是它包含 \n 字符。由于存在 \n 字符,我用来将字符串转换为 ios 的 Swifty json 库在转换时给我一个错误。我删除了 \n 字符并且它转换没有任何问题。因此,问题是在使用 ios 中的 swifty json 库从字符串转换为 json 格式时删除特殊转义字符,尽管我不知道其他库。