我的身份不会出现在我的表格中

时间:2016-05-24 10:52:22

标签: c# json

我想从json网址显示我的id,但不知何故它不会显示

private void Form2_Load(object sender, EventArgs e)
{
    Location = new Point(Screen.PrimaryScreen.WorkingArea.Right - Width,
    Screen.PrimaryScreen.WorkingArea.Bottom - Height);

    Label namelabel = new Label();
    namelabel.Location = new Point(13, 30);

    var json1 = new WebClient().DownloadString("http://dev.ibeaconlivinglab.com:1881/showemployeesbyhuurders?id=" + namelabel.Text);
    List <details> detailsList = JsonConvert.DeserializeObject<List<details>>(json1);

    foreach (details dets in detailsList)
    {            
        namelabel.Text = dets.id;
        this.Controls.Add(namelabel);

    }
}

public class details
{
    public string id { get; set; }
    public string name { get; set; }
    public int lID { get; set; }
    public string uuid { get; set; }
    public string wpUID { get; set; }
}

1 个答案:

答案 0 :(得分:0)

您在尝试阅读Label namelabel = new Label();后声明namelabel.Text和两行,并将其用作请求网址中的ID。那一刻它是空的。

另请注意,某些网站需要在http标头中指定UserAgent,以便执行此操作:

string jsonResult = "";
using (var wclient = new WebClient())
{
    wclient.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0";
    jsonResult = wclient.DownloadString("http://dev.ibeaconlivinglab.com:1881/showemployeesbyhuurders?id=" + someVarWithSomeID);
}