Jimmyt1988
Jimmyt1988

Reputation: 21166

C# api response comes back with backslashes before all json properties

When I get my response back from this:

    public IEnumerable<Organisation> GetAll()
    {
        string requestUrl = BlaBla.APIURL + "/org/";
        //string response = await postRequest.AuthenticatedGetData(requestUrl, BlablaDataContext.Contract.AccessToken).Result;
        AuthenticatedGetData(requestUrl, BlaBla.Contract.AccessToken);

        IEnumerable<Organisation> organisations = new Organisation[] {};
        return organisations;
    }

    public override void WebClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        string response = e.Result;
        using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(response)))
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Contract));
            Contract contract = (Contract)serializer.ReadObject(stream);
        }
    }

... string response = e.Result();

is returning the following:

response"[{\"status\":\"active\",\"segment\":null,\"contract_status\":\"none\",\"name\":\"Projects with James\",\"rights\...

How do I stop this from happening as I cannot use http://json2csharp.com/ to convert my json to csharp objects...

My webclient looks like this:

abstract public class PostRequest
{
    public void AuthenticatedGetData(string url, string accessToken)
    {
        WebClient client = new WebClient();
        //client.Headers["Content-Type"] = "application/json";
        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(this.WebClient_DownloadStringCompleted);
        client.DownloadStringAsync(new Uri(url + "?oauth_token=" + accessToken));
    }

    public abstract void WebClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e);
}

Upvotes: 0

Views: 2457

Answers (2)

wandos
wandos

Reputation: 1619

My Friend the backslash are appearing because your converting it to string! use : http://james.newtonking.com/json it is the best (in my opinion) Json lib for WP. its very easy to use and has ALOT of power.

Upvotes: 0

CSJ
CSJ

Reputation: 3961

Are you sure the string actually has backslashes in it, and it's not Visual Studio's visualizer doing it? If you punch that result out to a file, what's it look like?

The giveaway is that the string starts with a " ...

Upvotes: 3

Related Questions