이 문제에 관해 많은 질문을 읽었지 만 특정 문제를 해결할 수 없습니다.JsonConvert.SerializeObject 백 슬래시를 이스케이프 처리합니다.
웹 서비스 기능에서 json 문자열을 반환합니다.
나는 이러한 개체를 가지고 :
\uf0b1
\uf274
\uf185
\uf0fa
\uf0f4
\uf015
위 그림과 같이 그들은 정확하게 데이터베이스에 저장됩니다
public class WebServiceInitResult
{
public List<Activity> Activities { get; set; }
//rest of properties left out...
}
public class Activity
{
public string IconCode { get; set; }
//rest of properties left out...
}
IconCode
는 fontawesome 문자의 문자 코드,이 중 하나입니다. 나는 다음과 같은 httpReponse.Content
을 설정하면
는 백 슬래시 이스케이프 :
이httpResponseMessage.Content = new StringContent(JsonConvert.SerializeObject(webServiceInitResult), Encoding.UTF8, "application/json");
우편 배달부받은 JSON 응답은 다음과 같습니다 당신이 볼 수 있듯이
"activities": [
{
"ActivityCode": 2,
"DisplayValue": "Shopping",
"BackgroundColour": "E74C3C",
"IconCode": "\\uf0b1",
"ApplicationId": 2,
"Application": null,
"Id": 1,
"Active": true,
"DateCreated": "2016-11-25T10:15:40"
},
//rest of activities
]
는 IconCode
백 슬래시 이스케이프되었습니다 . 다른 질문을 읽으면서 직렬화하거나 응답을 보낼 때 이것이 Json.NET에서 발생하는지 확신 할 수 없습니다.
대신 ObjectContent
을 사용하여 해결하려고 했으므로 Json.NET을 피할 수 있었지만 같은 결과를 보냈습니다!
httpResponseMessage.Content = new ObjectContent(typeof(TravelTrackerWebServiceInitResult), webServiceInitResult, new JsonMediaTypeFormatter() , "application/json");
이제 막 달라 붙었습니다!
내가 원하는 것을 정확히 반환 할 수있는 더 좋은 방법이 있습니까? 문자는 앱에서 적절한 아이콘을 표시하는 데 사용됩니다.
추가 정보 : 나는 원래 하드 코딩이 값을했고, 모든 확인 작업 듯 :
webServiceInitResult.activities_TT = new List<Activity_TT>()
{
new Activity() { ActivityCode = 2, BackgroundColour = "E74C3C", DisplayValue="Shopping", IconCode="\uf0b1" },
new Activity() { ActivityCode = 3, BackgroundColour = "BF7AC5", DisplayValue="Running", IconCode="\uf274" },
new Activity() { ActivityCode = 4, BackgroundColour = "AF7AC5", DisplayValue="Walking", IconCode="\uf185" },
new Activity() { ActivityCode = 5, BackgroundColour = "3498DB", DisplayValue="Jogging", IconCode="\uf0fa" },
new Activity() { ActivityCode = 6, BackgroundColour = "2ECC71", DisplayValue="Resting", IconCode="\uf0f4" },
new Activity() { ActivityCode = 7, BackgroundColour = "F39C12", DisplayValue="Skipping", IconCode="\uf015" }
};
감사합니다.
* "IconCode는 fontawesome 문자에 대한 문자 코드이며 다음 중 하나입니다 : '\ uf0b1' \ uf274 \'\ uf185 \ '\ uf0fa \ uf0f4 \'\ uf015' 데이터베이스를 정확하게 보여줍니다. "* 그렇다면 이것이 문제입니다. '\','u','f','0','b','1' 문자로 저장되어 있다면, JSON 시리얼 라이저는 백 슬래시를 빠져 나가서 정확하게 옳은 일을하고 있습니다. 해당 유니 코드 이스케이프가 처리되기를 원한다면, 그것을 처리하기 전에'IconCode' 문자열에 할당해야합니다. 그래서 문자열은 실제로 유니 코드 이스케이프를위한 문자열이 아니라 문자를 포함합니다. –
'IconCode' 변수는 어떻게 설정하고 있습니까? C# 리터럴을 사용하면 게시 한 실제 Unicode 문자가 아닌 * 얻을 수 있습니다. 'Debug.Assert ("\ uf0b1".Length == 1)' –
데이터베이스는 유니 코드와 관련해서도 문제가 없으며,'nvarchar' 칼럼에 아무 것도 저장할 수 있습니다. 이스케이프 시퀀스를 실제 문자 대신에 6 개의 개별 문자로 저장하면 * 사용자 코드 *가 먼저 이스케이프 해제되어야합니다. 왜 유니 코드 열에 값을 저장하지 않습니까? –