Question

Im new in developing windows phone app --> i have problem so i have tried all the possible solutions but with no vain:- the application tried to take result from webservice the result come back like this:-

{"d":"{\"sessionid\":null,\"VersionInfo\":{\"Rel\":0,\"Ver\":0,\"Patch\":0,\"ForceUpdate\":0,\"UpdateType\":0,\"Globals\":{\"MultiSessionsAllowed\":true,\"CommCalcType\":1,\"PriceChangedTimer\":20,\"ValidLotsLocation\":2,\"CustumizeTradeMsg\":true,\"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\":\"HS Dev\",\"WLID\":\"3\",\"CheckWhiteLabel\":true,\"Password\":\"1234\",\"Username\":\"obeidat\",\"LastTickTime\":\"\/Date(1396613678728)\/\",\"SelectedAccount\":12345791,\"Name\":0,\"CompanyName\":\"HS Dev\",\"UserId\":579,\"DemoClient\":\"0\",\"FName\":\"obeidat\",\"SName\":null,\"TName\":null,\"LName\":null,\"Sms\":null,\"isReadOnly\":\"0\",\"SchSms\":\"0\",\"AlertSms\":\"0\",\"Temp\":null,\"GMTOffset\":\"5\",\"SvrGMT\":\"3\",\"ClientType\":null,\"EnableNews\":\"0\",\"PublicSlideNews\":\"\",\"PrivateSlideNews\":\"Thanks for using our platform##We will inform you here with any private news\",\"DealerTreePriv\":1}"}

so i have tried to parse it but it give me errors after that i have tried locally removed the {"d":" and \"LastTickTime\":\"\/Date(1396613678728)\/\" from the string with no connection to webservice and its working fine i use this code :-

   // Constructor
    public MainPage()
    {
        InitializeComponent();

        // Sample code to localize the ApplicationBar
        //BuildLocalizedApplicationBar();
    }

    //Json classes
    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 string 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; }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {

        var jsonString =
      "{\"sessionid\":null,\"VersionInfo\":{\"Rel\":0,\"Ver\":0,\"Patch\":0,\"ForceUpdate\":0,\"UpdateType\":0,\"Globals\":{\"MultiSessionsAllowed\":true,\"CommCalcType\":1,\"PriceChangedTimer\":20,\"ValidLotsLocation\":2,\"CustumizeTradeMsg\":true,\"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\":\"HS Dev\",\"WLID\":\"3\",\"CheckWhiteLabel\":true,\"Password\":\"1234\",\"Username\":\"obeidat\",\"SelectedAccount\":12345791,\"Name\":0,\"CompanyName\":\"HS Dev\",\"UserId\":-579,\"DemoClient\":\"0\",\"FName\":\"obeidat\",\"SName\":null,\"TName\":null,\"LName\":null,\"Sms\":null,\"isReadOnly\":\"0\",\"SchSms\":\"0\",\"AlertSms\":\"0\",\"Temp\":null,\"GMTOffset\":\"5\",\"SvrGMT\":\"3\",\"ClientType\":null,\"EnableNews\":\"0\",\"PublicSlideNews\":\"\",\"PrivateSlideNews\":\"Thanks for using our platform##We will inform you here with any private news\",\"DealerTreePriv\":1}";
        RootObject jsonObject = JsonConvert.DeserializeObject<RootObject>(jsonString);
        MessageBox.Show("hello " + jsonObject.Username + "" + jsonObject.UserId);

        int val1 = Convert.ToInt16(jsonObject.UserId);
        if (val1 > 0)

            MessageBox.Show("You are logedin");


        else
        {

            MessageBox.Show("Sorry Please login");
        }




    }




    //    // Create a new button and set the text value to the localized string from AppResources.
    //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
    //    appBarButton.Text = AppResources.AppBarButtonText;
    //    ApplicationBar.Buttons.Add(appBarButton);

    //    // Create a new menu item with the localized string from AppResources.
    //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
    //    ApplicationBar.MenuItems.Add(appBarMenuItem);
    //}



}

}

Please help me to parse all of the json result (locally i mean without conection to web)and what is the code to do that.

thank you all

Was it helpful?

Solution

First, your JSON is all a string property of "D". You can't deserialize it to RootClass. Second, your JSON contains invalid escape characters in the LastTickTime area. I removed both of these, and the JSON parses without errors of any kind. I just used a console app to test it.

     var s = "{\"sessionid\":null,\"VersionInfo\":{\"Rel\":0,\"Ver\":0,\"Patch\":0,\"ForceUpdate\":0,\"UpdateType\":0,\"Globals\":{\"MultiSessionsAllowed\":true,\"CommCalcType\":1,\"PriceChangedTimer\":20,\"ValidLotsLocation\":2,\"CustumizeTradeMsg\":true,\"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\":\"HS Dev\",\"WLID\":\"3\",\"CheckWhiteLabel\":true,\"Password\":\"1234\",\"Username\":\"obeidat\",\"SelectedAccount\":12345791,\"Name\":0,\"CompanyName\":\"HS Dev\",\"UserId\":579,\"DemoClient\":\"0\",\"FName\":\"obeidat\",\"SName\":null,\"TName\":null,\"LName\":null,\"Sms\":null,\"isReadOnly\":\"0\",\"SchSms\":\"0\",\"AlertSms\":\"0\",\"Temp\":null,\"GMTOffset\":\"5\",\"SvrGMT\":\"3\",\"ClientType\":null,\"EnableNews\":\"0\",\"PublicSlideNews\":\"\",\"PrivateSlideNews\":\"Thanks for using our platform##We will inform you here with any private news\",\"DealerTreePriv\":1}";

     var foo = JsonConvert.DeserializeObject<RootObject>(s);
     Console.WriteLine(foo.UserId); // Writes 579

UPDATE

If you have no control over what you get back, do the following: Take the response and get rid of the / combination. As such:

string webResponse = "your long web response from the server";
webResponse = webResponse.Replace(@"\/", "/");
// If dynamic isn't in the phone subset, you'll need a class here containing "d" as a string.
var jsonOuter = JsonConvert.DeserializeObject<dynamic>(webResponse);
var jsonInner = JsonConvert.DeserializeObject<RootObject>(jsonOuter.d);

if (jsonInner.UserId > .....)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top