0

I`m new in programming winodws 8 app , i have a webservice when i try to connect it using httprequest (Using URL with variables), this service return this:

{"d":"{\"sessionid\":\"twzv50okccwvgggeesjje2wa\",\"VersionInfo\":{\"Rel\":0,\"Ver\":0,\"Patch\":0,\"ForceUpdate\":0,\"UpdateType\":0,\"Globals\":{\"MultiSessionsAllowed\":true,\"CommCalcType\":2,\"PriceChangedTimer\":25,\"ValidLotsLocation\":2,\"CustumizeTradeMsg\":false,\"FirstWhiteLabeledOffice\":null,\"DealerTreePriv\":0,\"ClientConnectTimer\":200,\"ClientTimeoutTimer\":500,\"DefaultLots\":0.01,\"WebSecurityID\":\"agagag\",\"ServerGMT\":3}},\"SystemLockInfo\":{\"MinutesRemaining\":0,\"HoursRemaining\":0,\"DaysRemaining\":0,\"Maintanance\":0,\"WillBeLocked\":1},\"FirstWhiteLabel\":\"VertexFX 10\",\"WLID\":\"3\",\"CheckWhiteLabel\":true,\"Password\":\"1444\",\"Username\":\"test\",\"LastTickTime\":\"\/Date(1396307573431)\/\",\"SelectedAccount\":78821860,\"Name\":0,\"ServicePath\":null,\"GWSessionID\":\"56630\",\"IP\":\"Web (212.35.90.211)\",\"SessionDateStart\":\"01/04/2014 02:12:53\",\"CompanyName\":\"Hybrid Solutions\",\"UserId\":6119,\"DemoClient\":\"0\",\"FName\":\"omqrstu\",\"SName\":\"\",\"TName\":\"\",\"LName\":\"\",\"Sms\":null,\"isReadOnly\":\"0\",\"SchSms\":\"2\",\"AlertSms\":\"2\",\"Temp\":null,\"GMTOffset\":\"2\",\"SvrGMT\":\"3\",\"ClientType\":null,\"EnableNews\":\"1\",\"PublicSlideNews\":\"\",\"PrivateSlideNews\":\"Welcome to V 10\",\"DealerTreePriv\":1}"}

i have simple windows app with one button when i click the button i send the url with variables and i got the obj above , i want to use content of this object like UserID in if statement but with no vain i dont know how to use it in C# , can and body help me??

I use this code. I know it has many errors but I need someone guide me.

private void Button_Click(object sender, RoutedEventArgs e)
{
    String uriString = "url";
    var uri = new Uri(uriString);
    var httpWebRequest = HttpWebRequest.Create(uri);

    httpWebRequest.BeginGetResponse(new AsyncCallback(OnGettingResponse), httpWebRequest);
}

private void OnGettingResponse(IAsyncResult ar)
{
    var outerRoot = JsonConvert.DeserializeObject<OuterRootObject>( json );
    var root = JsonConvert.DeserializeObject<RootObject>( outerRoot.d );
    MessageBox.Show(root.UserId);
}
Craig W.
  • 17,838
  • 6
  • 49
  • 82
Jordan
  • 48
  • 7

1 Answers1

1

This is kind of a nasty situation. What you're getting back is a JSON object with a single property (i.e. d) and that property contains a string of JSON. So you basically need to unwrap the inner JSON from it's envelope. The following classes/code should work (using JSON.NET to do the deserialization).

public class OuterRootObject
{
    public string d { get; set; }
}

public class Globals
{
    public bool MultiSessionsAllowed { get; set; }
    public int CommCalcType { get; set; }
    public int PriceChangedTimer { get; set; }
    public int ValidLotsLocation { get; set; }
    public bool CustumizeTradeMsg { get; set; }
    public object FirstWhiteLabeledOffice { get; set; }
    public int DealerTreePriv { get; set; }
    public int ClientConnectTimer { get; set; }
    public int ClientTimeoutTimer { get; set; }
    public double DefaultLots { get; set; }
    public string WebSecurityID { get; set; }
    public int ServerGMT { get; set; }
}

public class VersionInfo
{
    public int Rel { get; set; }
    public int Ver { get; set; }
    public int Patch { get; set; }
    public int ForceUpdate { get; set; }
    public int UpdateType { get; set; }
    public Globals Globals { get; set; }
}

public class SystemLockInfo
{
    public int MinutesRemaining { get; set; }
    public int HoursRemaining { get; set; }
    public int DaysRemaining { get; set; }
    public int Maintanance { get; set; }
    public int WillBeLocked { get; set; }
}

public class RootObject
{
    public string sessionid { get; set; }
    public VersionInfo VersionInfo { get; set; }
    public SystemLockInfo SystemLockInfo { get; set; }
    public string FirstWhiteLabel { get; set; }
    public string WLID { get; set; }
    public bool CheckWhiteLabel { get; set; }
    public string Password { get; set; }
    public string Username { get; set; }
    public DateTime LastTickTime { get; set; }
    public int SelectedAccount { get; set; }
    public int Name { get; set; }
    public object ServicePath { get; set; }
    public string GWSessionID { get; set; }
    public string IP { get; set; }
    public string SessionDateStart { get; set; }
    public string CompanyName { get; set; }
    public int UserId { get; set; }
    public string DemoClient { get; set; }
    public string FName { get; set; }
    public string SName { get; set; }
    public string TName { get; set; }
    public string LName { get; set; }
    public object Sms { get; set; }
    public string isReadOnly { get; set; }
    public string SchSms { get; set; }
    public string AlertSms { get; set; }
    public object Temp { get; set; }
    public string GMTOffset { get; set; }
    public string SvrGMT { get; set; }
    public object ClientType { get; set; }
    public string EnableNews { get; set; }
    public string PublicSlideNews { get; set; }
    public string PrivateSlideNews { get; set; }
    public int DealerTreePriv { get; set; }
}

var outerRoot = JsonConvert.DeserializeObject<OuterRootObject>( json );
var root = JsonConvert.DeserializeObject<RootObject>( outerRoot.d );
Craig W.
  • 17,838
  • 6
  • 49
  • 82