将 Json Http 请求转换为 c# obj

本文关键字:obj 转换 请求 Json Http | 更新日期: 2023-09-27 17:56:07

我是编写winodws 8应用程序的新手,当我尝试使用httprequest(使用带有变量的URL)连接它时,我有一个网络服务,该服务返回以下内容:

{"

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''",''"用户名":''"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''",''"公司名称":"混合解决方案","用户 ID":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}"}

我有一个简单的 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);
}

将 Json Http 请求转换为 c# obj

这是一种令人讨厌的情况。您返回的是一个具有单个属性(即 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 );