我需要解析windows phone app c#中的json结果

时间:2014-04-04 12:26:41

标签: c# json web-services windows-phone-8 json.net

我是开发Windows手机应用程序的新手 - >我有问题,所以我尝试了所有可能的解决方案,但没有白费: - 应用程序尝试从webservice获取结果,结果如下: -

{ “d”: “{\” 的sessionid \ “:空,\” VERSIONINFO \ “:{\” 相对\ “:0,\” 版\ “:0,\” 补丁\“:0,\ “ForceUpdate \”:0,\ “更新类型\”:0,\ “全局\”:{\ “MultiSessionsAllowed \”:真,\ “CommCalcType \”:1,\ “PriceChangedTimer \”:20,\“ValidLotsLocation \ “:2,\” CustumizeTradeMsg \ “:真,\” FirstWhiteLabeledOffice \ “:空,\” 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 \ “:真实的,\” 密码\ “:\” 1234 \ “\ ”用户名\“:\ ”奥贝达特\“,\ ”LastTickTime \“:\ ”\ /日期(1396613678728)\ / \“,\” SelectedAccount \“:12345791,\”Name \“:0,\”CompanyName \“:\”HS Dev \“,\”UserId \“:579,\”DemoClient \“:\”0 \“,\”FName \ “:\” 奥贝达特\ “\ ”SNAME \“:空,\ ”TNAME \“:空,\ ”LName的\“:空,\ ”SMS \“:空,\ ”isReadOnly \“:\” 0 \ “\ ”SchSms \“:\ ”0 \“,\ ”AlertSms \“:\ ”0 \“,\ ”TEMP \“:空,\ ”GMTOffset \“:\ ”5 \“,\” SvrGMT \ “:\” 3 \“,\”ClientType \“:null,\”EnableNews \“:\”0 \“,\”PublicSlideNews \“:\”\“,\”PrivateSlideNews \“:\”感谢您使用我们的平台## We将通知您任何私人新闻\“,\”DealerTreePriv \“:1}”}

所以我试图解析它,但是在我尝试本地删除了{“d”:“和\”LastTickTime \“:\”\ / Date(1396613678728)\ / \“之后它给了我错误没有连接到webservice的字符串及其工作正常我使用此代码: -

   // 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);
    //}



}

}

请帮我解析所有的json结果(本地意思是没有连接到web)以及代码是什么。

谢谢大家

1 个答案:

答案 0 :(得分:0)

首先,您的JSON是&#34; D&#34;的所有字符串属性。您无法将其反序列化为RootClass。其次,您的JSON在LastTickTime区域中包含无效的转义字符。我删除了这两个,并且JSON解析没有任何错误。我只是用一个控制台应用来测试它。

     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

更新

如果您无法控制所取得的内容,请执行以下操作: 采取响应并摆脱/组合。就这样:

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 > .....)