1

I am developing a Windows Phone 8 C# app for the 1st time.

I am calling my web method I have defined in an aspx code-behind class.

How do I parse the object returned please?

This is my return object:

public class ResponseObject
{
    public bool Success;
}

This is my test web method:

[WebMethod]
public static ResponseObject Test(string username, string password)
{
    ResponseObject responseObject = new ResponseObject();

    responseObject.Success= true;

    return responseObject;
}

This is my calling client code:

    private async void LogIn()
    {
        using (var client = new HttpClient())
        {
            var resp = await client.PostAsJsonAsync("http://my ip/UserManagement/Login.aspx/Test",
                                                     new { username = "", password = "" });
            var str = await resp.Content.ReadAsStringAsync();
        }
    }

This what the value of str looks like:

{"d":{"__type":"LogIn+ResponseObject","Success":true}}

I guess I could parse the string myself but does JSON offer a way to do this a bit more cleanly?

Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179
  • 1
    possible duplicate of [Deserialize JSON with C#](http://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp) – tnw Nov 18 '14 at 17:25
  • @tnw thank you. i should have looked harder. I will leave this up for a few mins to make sure you read this thank you and will delete the question – Andrew Simpson Nov 18 '14 at 17:29
  • 1
    No worries, sometimes it's just knowing the right thing to search for, "deserialize" is the key here :) – tnw Nov 18 '14 at 17:29
  • 1
    If you also have `ResponseObject` on the client, you can deserialize the JSON into an instance using `DataContractSerializer` or JSON.Net. – Mike Christensen Nov 18 '14 at 17:30
  • @MikeChristensen very informative. So, i guess I would have to put that class object into a separate assembly so both client and server would recognize it or would I use something like AutoMapper? – Andrew Simpson Nov 18 '14 at 17:36
  • Haven't done it in a while, but I think you're supposed to use [svcutil.exe](http://msdn.microsoft.com/en-us/library/aa751905.aspx) to generate proxy classes from your web service, then compile that code.. It'll basically build stubs of all your data types without any of the internal logic. – Mike Christensen Nov 18 '14 at 17:48
  • BTW, Visual Studio will do this for you if you right click on your Windows Phone project references and select *Add Service Reference*, then enter the URL of your web service. – Mike Christensen Nov 18 '14 at 17:50
  • @MikeChristensen Hi, I cannot find 'Add Service' when I right-click. It does not seem to be available from WP8? – Andrew Simpson Nov 18 '14 at 17:52
  • 1
    Hmm yea maybe I'm incorrect or it's not available for Windows Phone projects. You might have to use SvcUtil.exe directly. – Mike Christensen Nov 18 '14 at 18:00
  • @tnw I cannot close it now. I hope you do not mind..? – Andrew Simpson Nov 18 '14 at 18:04

1 Answers1

2

Using Json.Net

var str = await resp.Content.ReadAsStringAsync();
var jsonObj = JsonConvert.DeserializeObject<Response>(str);

public class D
{
    public string __type { get; set; }
    public bool Success { get; set; }
}

public class Response
{
    public D d { get; set; }
}

For future use cases you can define your own extension method

public static class SOExtensions
{
    public static async Task<T> ReadAsJsonAsync<T>(this HttpContent content)
    {
        var json = await content.ReadAsStringAsync();
        return JsonConvert.DeserializeObject<T>(json);
    }

}
EZI
  • 15,209
  • 2
  • 27
  • 33
  • HI, thanks. This is new. will give this a go right now. thanks – Andrew Simpson Nov 18 '14 at 17:37
  • hi, I get this error: System.Net.Http.HttpContent does not contain a definition for ReadAsJsonAsync and no extension method 'ReadAsJsonAsync' – Andrew Simpson Nov 18 '14 at 17:43
  • I added using System.Net.Http.Formatting; but still get the same error – Andrew Simpson Nov 18 '14 at 17:44
  • @AndrewSimpson I tested it on my PC. Seems like WP 8 doesn't support that method. I'll update my answer with Json.Net – EZI Nov 18 '14 at 17:47
  • @AndrewSimpson Updated....(I found your error. It was my own extension method :) sorry). – EZI Nov 18 '14 at 17:50
  • hmmm, it breaks with an error ina file called App.g.i.cs. It tells me :Windows.UI.Xaml.UnhandledExceptionEventArgs e) – Andrew Simpson Nov 18 '14 at 17:55
  • implies objects do not match up? I am guessing here. I have not changed the class object in my server though in anyway. Should I? – Andrew Simpson Nov 18 '14 at 17:56
  • @AndrewSimpson Sorry, I don't have any idea about your error, but `Response` is a very common class name, You may use it somewhere else, Rename it to something else. – EZI Nov 18 '14 at 17:58
  • HI, I am an idiot, I was calling the wrong method which was returning the wrong object. lol. thanks for all your help :) – Andrew Simpson Nov 18 '14 at 18:04