2015-10-12 79 views
-1

我正嘗試從我的應用程序創建JSON &將其發佈到我的服務器。下面的代碼工作正常,直到我把我的語法改爲Swift 2.在Swift中將字典轉換爲JSON顯示許多「\」

現在我弄不明白。問題來自哪裏?

// Dictionary 

    jsonDic = ["REG_ID": Regid!, "COURSE_ID": CourseID, "QUESTION_ID": sqid, "EXAM_CHAPTER_ID": ECID[index], "EXAM_RESULT_ID": ID, "USER_ANSWER": userans, "USER_TIMING": usertime, "REVIEWED": MFR, "RIGHT_ANSWER": right] 

// Dictionary to JSON 

    let theJSONData = try? NSJSONSerialization.dataWithJSONObject(
        jsonDic , 
        options: NSJSONWritingOptions(rawValue: 0)) 
       let theJSONText = NSString(data: theJSONData!, 
        encoding: NSASCIIStringEncoding) 
       JSONArray.append(theJSONText! as String) 
       print(JSONArray) 

輸出

["{\"USER_ANSWER\":\"1188\",\"QUESTION_ID\":\"391\",\"REG_ID\":\"2\",\"COURSE_ID\":\"111\",\"EXAM_RESULT_ID\":\"000\",\"USER_TIMING\":\"33\",\"REVIEWED\":\"\",\"RIGHT_ANSWER\":\"\",\"EXAM_CHAPTER_ID\":\"17\"}", 
"{\"USER_ANSWER\":\"1195\",\"QUESTION_ID\":\"395\",\"REG_ID\":\"2\",\"COURSE_ID\":\"111\",\"EXAM_RESULT_ID\":\"000\",\"USER_TIMING\":\"2\",\"REVIEWED\":\"\",\"RIGHT_ANSWER\":\"Y\",\"EXAM_CHAPTER_ID\":\"17\"}", 
"{\"USER_ANSWER\":\"1157\",\"QUESTION_ID\":\"380\",\"REG_ID\":\"2\",\"COURSE_ID\":\"111\",\"EXAM_RESULT_ID\":\"000\",\"USER_TIMING\":\"2\",\"REVIEWED\":\"\",\"RIGHT_ANSWER\":\"\",\"EXAM_CHAPTER_ID\":\"17\"}", 
"{\"USER_ANSWER\":\"1180\",\"QUESTION_ID\":\"387\",\"REG_ID\":\"2\",\"COURSE_ID\":\"111\",\"EXAM_RESULT_ID\":\"000\",\"USER_TIMING\":\"1\",\"REVIEWED\":\"\",\"RIGHT_ANSWER\":\"Y\",\"EXAM_CHAPTER_ID\":\"17\"}", 
"{\"USER_ANSWER\":\"1217\",\"QUESTION_ID\":\"400\",\"REG_ID\":\"2\",\"COURSE_ID\":\"111\",\"EXAM_RESULT_ID\":\"000\",\"USER_TIMING\":\"2\",\"REVIEWED\":\"\",\"RIGHT_ANSWER\":\"\",\"EXAM_CHAPTER_ID\":\"17\"}", 
"{\"USER_ANSWER\":\"\",\"QUESTION_ID\":\"401\",\"REG_ID\":\"2\",\"COURSE_ID\":\"111\",\"EXAM_RESULT_ID\":\"000\",\"USER_TIMING\":\"0\",\"REVIEWED\":\"\",\"RIGHT_ANSWER\":\"\",\"EXAM_CHAPTER_ID\":\"17\"}", 
"{\"USER_ANSWER\":\"\",\"QUESTION_ID\":\"383\",\"REG_ID\":\"2\",\"COURSE_ID\":\"111\",\"EXAM_RESULT_ID\":\"000\",\"USER_TIMING\":\"0\",\"REVIEWED\":\"\",\"RIGHT_ANSWER\":\"\",\"EXAM_CHAPTER_ID\":\"17\"}", 
"{\"USER_ANSWER\":\"\",\"QUESTION_ID\":\"396\",\"REG_ID\":\"2\",\"COURSE_ID\":\"111\",\"EXAM_RESULT_ID\":\"000\",\"USER_TIMING\":\"0\",\"REVIEWED\":\"\",\"RIGHT_ANSWER\":\"\",\"EXAM_CHAPTER_ID\":\"17\"}", 
"{\"USER_ANSWER\":\"\",\"QUESTION_ID\":\"398\",\"REG_ID\":\"2\",\"COURSE_ID\":\"111\",\"EXAM_RESULT_ID\":\"000\",\"USER_TIMING\":\"0\",\"REVIEWED\":\"\",\"RIGHT_ANSWER\":\"\",\"EXAM_CHAPTER_ID\":\"17\"}", 
"{\"USER_ANSWER\":\"\",\"QUESTION_ID\":\"382\",\"REG_ID\":\"2\",\"COURSE_ID\":\"111\",\"EXAM_RESULT_ID\":\"000\",\"USER_TIMING\":\"0\",\"REVIEWED\":\"\",\"RIGHT_ANSWER\":\"\",\"EXAM_CHAPTER_ID\":\"17\"}"] 

預期輸出

[{"USER_TIMING":"4","COURSE_ID":"93","USER_ANSWER":"4726","EXAM_RESULT_ID":"901","REG_ID":"1","QUESTION_ID":"1378","RIGHT_ANSWER":"","EXAM_CHAPTER_ID":"35","REVIEWED":""}, 
    {"USER_TIMING":"2","COURSE_ID":"93","USER_ANSWER":"50856","EXAM_RESULT_ID":"901","REG_ID":"1","QUESTION_ID":"13491","RIGHT_ANSWER":"","EXAM_CHAPTER_ID":"35","REVIEWED":""}, 
    {"USER_TIMING":"2","COURSE_ID":"93","USER_ANSWER":"50882","EXAM_RESULT_ID":"901","REG_ID":"1","QUESTION_ID":"13497","RIGHT_ANSWER":"","EXAM_CHAPTER_ID":"35","REVIEWED":""}, 
    {"USER_TIMING":"2","COURSE_ID":"93","USER_ANSWER":"4712","EXAM_RESULT_ID":"901","REG_ID":"1","QUESTION_ID":"1375","RIGHT_ANSWER":"Y","EXAM_CHAPTER_ID":"35","REVIEWED":""}, 
    {"USER_TIMING":"4","COURSE_ID":"93","USER_ANSWER":"50832","EXAM_RESULT_ID":"901","REG_ID":"1","QUESTION_ID":"13482","RIGHT_ANSWER":"","EXAM_CHAPTER_ID":"35","REVIEWED":""}, 
    {"USER_TIMING":"3","COURSE_ID":"93","USER_ANSWER":"50876","EXAM_RESULT_ID":"901","REG_ID":"1","QUESTION_ID":"13496","RIGHT_ANSWER":"","EXAM_CHAPTER_ID":"35","REVIEWED":""}, 
    {"USER_TIMING":"4","COURSE_ID":"93","USER_ANSWER":"4686","EXAM_RESULT_ID":"901","REG_ID":"1","QUESTION_ID":"1368","RIGHT_ANSWER":"","EXAM_CHAPTER_ID":"35","REVIEWED":"Y"}, 
    {"USER_TIMING":"4","COURSE_ID":"93","USER_ANSWER":"4707","EXAM_RESULT_ID":"901","REG_ID":"1","QUESTION_ID":"1373","RIGHT_ANSWER":"","EXAM_CHAPTER_ID":"35","REVIEWED":"Y"}, 
    {"USER_TIMING":"0","COURSE_ID":"93","USER_ANSWER":"","EXAM_RESULT_ID":"901","REG_ID":"1","QUESTION_ID":"1367","RIGHT_ANSWER":"","EXAM_CHAPTER_ID":"35","REVIEWED":""}, 
    {"USER_TIMING":"0","COURSE_ID":"93","USER_ANSWER":"","EXAM_RESULT_ID":"901","REG_ID":"1","QUESTION_ID":"1369","RIGHT_ANSWER":"","EXAM_CHAPTER_ID":"35","REVIEWED":""}] 
+0

你的'JSONArray'是什麼? 'String'不包含'append'字符串方法。 @EricD。我只是嘗試打印'theJSONText'字符串,它不包含任何'\\'字符(當* print *時,'\\'字符會被自動刪除,可能是'JSONArray'變量存在問題 – t4nhpt

+0

I請問@ user5184878關於他的'JSONArray'變量,你名字後面的部分是給你的,@Eric D. – t4nhpt

+0

首先,我猜'JSONArray'是一個'String',但它不是(因爲String沒有方法可以是一個json變量類型 – t4nhpt

回答

0

正如埃裏克認爲,這是何等的預期發生。它被稱爲escape character

包括Swift在內的許多語言都使用\來標記需要轉義的字符。如果您確實想要代表\,則必須使用\\以避免混淆。

+0

我怎樣才能打印它我的控制檯沒有這些Escape序列「/」 – user5184878

+0

我不這麼認爲'NSJSONSerialization'提供了'prettyprint'之類的東西。你可以使用[SwiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON)。它更優雅,更健壯。此外,它可以做漂亮的打印 – skyline75489