0
如果JSON具有「/ n」和「/」,則我的php將返回錯誤。我不確定我需要更改我的PHP代碼或我的swift代碼。如何在Swift或Php中將核心數據編碼爲JSON而不使用斜線
我的PHP代碼
$json = file_get_contents('php://input');
$obj = json_decode($json, true);
print_r($obj);
它看起來像無法解碼?
Array ([0] => {"user":"1","accbooktype":"æµæ°´è´¦æœ¬","accbookname":"日常","accbookid":"1","category":"日常","cid":"U1L2B555"})
成功輸出:
Array ([0] => Array ([cid] => 5 [accbookname] => test [accbooktype] => test [category] => test [user] => test))
這是我的SWIFT代碼。如果在這裏更改代碼。我需要知道如何將核心數據編碼爲不帶斜槓的JSON,如何?
//newdict is Core data
let jsonData = try JSONSerialization.data(withJSONObject: newdict, options: [.prettyPrinted])
//I tried this also but not worked
//let jsonEncoder = JSONEncoder()
//let jsonData = try jsonEncoder.encode(newdict)
let jsonString = String(data: jsonData, encoding: .utf8)
bookJSON.append(jsonString!)
print(bookJSON)
輸出:
["{\n \"cid\" : \"U1L2B1\",\n \"user\" : \"1\",\n \"accbooktype\" : \"流水賬本\",\n \"accbookname\" : \"日常\",\n \"accbookid\" : \"1\",\n \"category\" : \"日常\"\n}"]
對此
[
{
"cid": "5",
"accbookname" : "test",
"accbooktype": "test",
"category": "test",
"user": "test"
}
]
你已經從file_get_contents('php:// input')獲得了兩次編碼的json; 刪除另一端的json編碼,在那裏您調用端點並傳遞數據。另外,由於特殊字符的原因,在傳遞到端點之前,您可能需要將數據編碼爲utf8。 – Eimsas
我不需要在Swift中將字典編碼爲JSON? @Eimsas –