I've been reading loads of questions about this issue but can't seem to resolve my particular issue.
I'm returning a json string from a web service function.
I have these objects:
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...
}
The IconCode
is the character code for a fontawesome character, any one of these:
\uf0b1
\uf274
\uf185
\uf0fa
\uf0f4
\uf015
They are stored in a database exactly as shown above.
When I set the httpReponse.Content
like below, the backslash is escaped:
httpResponseMessage.Content = new StringContent(JsonConvert.SerializeObject(webServiceInitResult), Encoding.UTF8, "application/json");
The json response received by PostMan is:
"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
]
As you can see, the IconCode
backslash has been escaped. From reading other questions, I'm unable to confidently decide if this is happening by Json.NET when I serialize or when then response is sent.
I tried to resolve using ObjectContent
instead so I could avoid Json.NET but it returned the same!
httpResponseMessage.Content = new ObjectContent(typeof(TravelTrackerWebServiceInitResult), webServiceInitResult, new JsonMediaTypeFormatter() , "application/json");
Now I am stuck!
Is there a better way to do this that will return exactly what I need? The characters are used by an app to display the appropriate icon.
Extra info: I originally had these values hardcoded and everything seemed to work ok:
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" }
};
Thanks.