我是编程winodws 8应用程序的新手,当我尝试使用httprequest连接它时使用web服务(使用带变量的URL),此服务返回:
{ “d”: “{\” 的sessionid \ “:\” twzv50okccwvgggeesjje2wa \”,\ “VERSIONINFO \”:{\ “相对\”:0,\ “版\”:0,\ “补丁\” :0,\ “ForceUpdate \”:0,\ “更新类型\”:0,\ “全局\”:{\ “MultiSessionsAllowed \”:真,\ “CommCalcType \”:2,\ “PriceChangedTimer \”:25, \ “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 \”:\“VertexFX 10 \”,\“WLID \”:\“3 \” ,\ “CheckWhiteLabel \”:真,\ “密码\”:\ “1444 \”,\ “用户名\”:\ “测试\”,\ “LastTickTime \”:\“\ /日期(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 \”:空,\ “isReadOnly \”:\ “0 \” ,\ “SchSms \”:\ “2 \” \ “AlertSms \”:\ “2 \” \ “TEMP \”:空,\ “GMTOffset \”:\ “2 \” \ “SvrGMT \” :\“3 \”,\“ClientType \”:null,\“EnableNews \”:\“1 \”,\“PublicSlideNews \”:\“\”,\“PrivateSlideNews \”:\“欢迎来到V 10 \ “\ ”DealerTreePriv \“:1}”}
我有简单的Windows应用程序,只需一个按钮,当我点击按钮我发送带变量的URL,我得到了上面的obj,我想在if语句中使用像UserID这样的对象的内容,但没有白白我不知道如何在C#中使用它,可以和身体帮助我?
我使用此代码。我知道它有很多错误,但我需要有人指导我。
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);
}
答案 0 :(得分:1)
这是一种令人讨厌的情况。你得到的是一个带有单个属性的JSON对象(即d
),该属性包含一个JSON字符串。所以你基本上需要从它的信封中解开内部JSON。以下类/代码应该有效(使用JSON.NET进行反序列化)。
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 );