我正在尝试将以下字符串转换为使用swift json在ios中从服务器进入json数组的字符串响应。但它没有被转换成字符串。
这是我的字符串:
[{\ “斯诺\”:\ “1 \” \ “Grivence_Code \”:\ “5511 \”,\ “CaseType \”:\ “ACP \”,\ “MarkedTo \”:\“DGSE \ “\ ”Applied_Date \“:\ ”09君-2016 \“,\ ”描述\“:\ ”hahahha \“,\ ”CurrentStatus \“:\” 弃置 关\”,\ “Action_Code \”:\ “6290 \”,\ “Action_TakenBy \”:\ “DGSE \”,\ “ACTION_DATE \”:\ “28军-2016 \”,\ “备注\”: \ “\” \ “FacultyInfo_Code \”:\ “107406 \” \ “GrievanceNumber \”:\ “10740688052 \”},{\ “斯诺\”:\ “2 \” \ “Grivence_Code \”:\ “5516 \”,\ “CaseType \”:\ “ACP \”,\ “MarkedTo \”:\ “DGSE \”,\ “Applied_Date \”:\ “09君-2016 \”,\ “描述\” :\ “\ n \ n \ n \ n \ n \ n \”,\ “CurrentStatus \”:\“弃置 关\”,\ “Action_Code \”:\ “6290 \”,\ “Action_TakenBy \”:\ “DGSE \”,\ “ACTION_DATE \”:\ “28月-2016 \”,\ “备注\”: \ “\” \ “FacultyInfo_Code \”:\ “107406 \” \ “GrievanceNumber \”:\ “10740674400 \”},{\ “斯诺\”:\ “3 \” \ “Grivence_Code \”:\ “5522 \”,\ “CaseType \”:\ “ACP \”,\ “MarkedTo \”:\ “DGSE \”,\ “Applied_Date \”:\ “12君-2016 \”,\ “描述\” :\“测试 测试测试测试测试 \ “\ ”CurrentStatus \“:\” 弃置 关\”,\ “Action_Code \”:\ “6290 \”,\ “Action_TakenBy \”:\ “DGSE \”,\ “ACTION_DATE \”:\ “28军-2016 \”,\ “备注\”: \ “\” \ “FacultyInfo_Code \”:\ “107406 \” \ “GrievanceNumber \”:\ “10740663938 \”},{\ “斯诺\”:\ “4 \” \ “Grivence_Code \”:\ “5529 \”,\ “CaseType \”:\ “ACP \”,\ “MarkedTo \”:\ “CEO(Nabha)\”,\ “Applied_Date \”:\ “6月13日2016 \”,\”说明\ “:\” 测试。 \ “\ ”CurrentStatus \“:\ ”待\“,\ ”Action_Code \“:\ ”\“ \ ”Action_TakenBy \“:\ ”CEO(Nabha)\“,\ ”ACTION_DATE \“:\” \ ” \ “备注\”:\ “\” \ “FacultyInfo_Code \”:\ “107406 \” \ “GrievanceNumber \”:\ “10740659500 \”},{\ “斯诺\”:\ “5 \” ,\ “Grivence_Code \”:\ “5530 \”,\ “CaseType \”:\ “ACP \”,\ “MarkedTo \”:\ “DGSE \”,\ “Applied_Date \”:\“6月13日2016 \ “\ ”描述\“:\ ”bbbnnnnnnbbbvvvvvvbbbbvbbbb \“,\ ”CurrentStatus \“:\” 弃置 关\”,\ “Action_Code \”:\ “6290 \”,\ “Action_TakenBy \”:\ “DGSE \”,\ “ACTION_DATE \”:\ “28月-2016 \”,\ “备注\”: \ “\” \ “FacultyInfo_Code \”:\ “107406 \” \ “GrievanceNumber \”:\ “10740647081 \”},{\ “斯诺\”:\ “6 \” \ “Grivence_Code \”:\ “5531 \”,\ “CaseType \”:\ “ACP \”,\ “MarkedTo \”:\ “DGSE \”,\ “Applied_Date \”:\ “6月13日2016 \”,\ “描述\” :\” vvbhhhhhhhhhhgvg。 vvbhhhhhhhhhhgvg。 \ “\ ”CurrentStatus \“:\” 弃置 关\”,\ “Action_Code \”:\ “6290 \”,\ “Action_TakenBy \”:\ “DGSE \”,\ “ACTION_DATE \”:\ “28月-2016 \”,\ “备注\”: \ “\” \ “FacultyInfo_Code \”:\ “107406 \” \ “GrievanceNumber \”:\ “10740634790 \”},{\ “斯诺\”:\ “7 \”,\ “Grivence_Code \”:\ “5532 \”,\ “CaseType \”:\ “ACP \”,\ “MarkedTo \”:\ “DGSE \”,\ “Applied_Date \”:\ “6月13日2016 \”,\ “描述\” :\“一世 我在 prakshvtadavhjhgggggggggggghggghhhg \ “\ ”CurrentStatus \“:\” 弃置 关\”,\ “Action_Code \”:\ “6290 \”,\ “Action_TakenBy \”:\ “DGSE \”,\ “ACTION_DATE \”:\ “28月-2016 \”,\ “备注\”: \ “\” \ “FacultyInfo_Code \”:\ “107406 \” \ “GrievanceNumber \”:\ “10740678909 \”},{\ “斯诺\”:\ “8 \”,\ “Grivence_Code \”:\ “5728 \”,\ “CaseType \”:\“增量 有关\ “\ ”MarkedTo \“:\ ”CEO(贾朗达尔)\“,\ ”Applied_Date \“:\ ”01 - 8-2016 \“,\ ”描述\“:\” 严谨 测试是开发强大软件的最重要步骤 \ “\ ”CurrentStatus \“:\” 弃置 关\ “\ ”Action_Code \“:\ ”6718 \“,\ ”Action_TakenBy \“:\ ”CEO(贾朗达尔)\“,\ ”ACTION_DATE \“:\ ”12月-2016 \“,\” 答记者问\ “:\” \ “\ ”FacultyInfo_Code \“:\ ”107406 \“ \ ”GrievanceNumber \“:\ ”10740611379 \“},{\ ”斯诺\“:\ ”9 \“,\” Grivence_Code \ “:\” 5884 \ “\ ”CaseType \“:\ ”ACP \“,\ ”MarkedTo \“:\ ”DGSE \“,\ ”Applied_Date \“:\ ”10月-2016 \“,\”说明\ “:\” hellovvnnbvccccdccxxxccccccvv \ “\ ”CurrentStatus \“:\” 弃置 关\”,\ “Action_Code \”:\ “6290 \”,\ “Action_TakenBy \”:\ “DGSE \”,\ “ACTION_DATE \”:\ “12月-2016 \”,\ “备注\”: \ “\” \ “FacultyInfo_Code \”:\ “107406 \” \ “GrievanceNumber \”:\ “10740697305 \”},{\ “斯诺\”:\ “10 \”,\ “Grivence_Code \”:\ “5885 \”,\ “CaseType \”:\“试用 期间相关\“,\”MarkedTo \“:\”导演 (SCERT)\ “\ ”Applied_Date \“:\ ”10九月2016 \“,\ ”描述\“:\ ”vgccgggcgggggvbvvhbbvvbbbbbbbb \“,\ ”CurrentStatus \“:\ ”待\“,\” Action_Code \ “:\” \ “\ ”Action_TakenBy \“:\” 导演 (SCERT)\ “\ ”ACTION_DATE \“:\ ”\“ \ ”备注\“:\ ”\“ \ ”FacultyInfo_Code \“:\ ”107406 \“ \ ”GrievanceNumber \“:\” 10740686213 \ “},{\” 斯诺\ “:\” 11 \ “\ ”Grivence_Code \“:\ ”6176 \“,\ ”CaseType \“:\ ”ACR \“,\ ”MarkedTo \“:\” DEO( EE)-BATHINDA \ “\ ”Applied_Date \“:\ ”05-JAN-2017 \“,\ ”描述\“:\ ”Addedczxvxvxvdzvdgdsdgdsxgdgdsgdfgdsgdss \“,\ ”CurrentStatus \“:\ ”待\“,\” Action_Code \ “:\” \ “\ ”Action_TakenBy \“:\ ”DEO(EE)-BATHINDA \“,\ ”ACTION_DATE \“:\ ”\“ \ ”备注\“:\ ”\“ \” 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.请帮助。
答案 0 :(得分: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);
[
答案 1 :(得分:0)
我终于找到了解决自己问题的方法。 json从字符串转换的问题是它包含\ n字符。我用来将字符串转换为ios的Swifty json库在转换时由于存在\ n characer而给出错误。我删除了\ n字符并且它没有任何问题地转换。因此,当使用ios中的swifty json库从字符串转换为json格式时,删除特殊的转义字符虽然我不知道其他库。