我如何循环json url [英] How do I loop through a json url

查看:51
本文介绍了我如何循环json url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

0
下来投票

最爱

我有一个json Url,但我有更多的Forenames。现在它只显示一个Forename。



公共类详情

{



public string id {get;组; }

公共字符串名称{get;组; } $ / $
public int lID {get;组; }

public string uuid {get;组; }

public string wpUID {get;组; }

public string fname {get;组; }



}

private void Form2_Load(object sender,EventArgs e){



var json1 = new WebClient()。DownloadString(http://dev.ibeaconlivinglab.com:1881/showemployeesbyhu

urders?id =+ companyID);





列表< details> detailsList = JsonConvert.DeserializeObject< list>< details>>(json1);



foreach(详情详见dets1)

{



label3.Text = dets1.fname;

this.Controls.Add(label3);

< br $>
}

}



Json:



[{id:1,fname:Jeff,},{id:1,fname:Jan,},{id:1,fname:Piet ,}]



我的尝试:



foreach(详情dets1 in detailsList)

{



var label = new Label();

label.Name = dets1 .fname;

label.Text = dets1.fname;



this.Controls.Add(label);

}

解决方案

当您添加标签时,它们只会相互叠加,因此您只能看到一个。您需要确保添加标签以使它们不重叠



  int  x =  10 ; 
int y = 10 ;

foreach (详情dets1 in detailsList)
{
var label = new Label();
label.AutoSize = true ;
label.Location = new Point(x,y);
label.Name = dets1.fname;
label.Text = dets1.fname;

this .Controls.Add(label);

y + = label.Height + 5 ;
}





或者更好的是将它们添加到更合适的控件中。


尝试使用 Json.NET - Newtonsoft [ ^ ]



创建一个属性与json字符串匹配的类

< pre lang =C#> class MyType
{
public 字符串 id { get ; set ; }
public string fname { get ; set ; }
}



并使用Deserialise方法可以将其转换为类型列表

 < span class =code-keyword> string  jsondata =   [{\id\ :1,\fname \:\Jeff \,},{\id \:1,\fname \:\Jan \,}, {\id \:1,\fname \:\Piet \,}]; 
var items = JsonConvert.DeserializeObject< List< MyType>>(jsondata);
foreach var item in 项目)
{
string id = item.id;
string name = item.fname;
}


0 down vote
favorite
I got a json Url but I've got more Forenames in it. Right now it only shows one Forename.

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; }
public string fname { get; set; }

}
private void Form2_Load(object sender, EventArgs e){

var json1 = new WebClient().DownloadString("http://dev.ibeaconlivinglab.com:1881/showemployeesbyhu
urders?id=" + companyID);


List<details> detailsList = JsonConvert.DeserializeObject<list><details>>(json1);

foreach (details dets1 in detailsList)
{

label3.Text = dets1.fname;
this.Controls.Add(label3);

}
}

Json :

[ { "id": 1, "fname": "Jeff", }, { "id": 1, "fname": "Jan", }, { "id": 1, "fname": "Piet", } ]

What I have tried:

foreach (details dets1 in detailsList)
{

var label = new Label();
label.Name = dets1.fname;
label.Text = dets1.fname;

this.Controls.Add(label);
}

解决方案

When you add the labels they just get added on top of each other so you only see the one. You need to make sure the labels are added such that they don't overlap

int x = 10;
int y = 10;

foreach (details dets1 in detailsList)
{
    var label = new Label();
    label.AutoSize = true;
    label.Location = new Point(x, y);
    label.Name = dets1.fname;
    label.Text = dets1.fname;

    this.Controls.Add(label);

    y += label.Height + 5;
} 



Or better yet add them to a more appropriate control.


try this using Json.NET - Newtonsoft[^]

Create a class with properties matching your json string

class MyType
       {
           public string id { get; set; }
           public string fname { get; set; }
       }


and using Deserialise method can convert it to its list of Type

string jsondata = "[{\"id\": 1,\"fname\": \"Jeff\",},{\"id\": 1,\"fname\": \"Jan\",},{\"id\": 1,\"fname\": \"Piet\",}]";
var items = JsonConvert.DeserializeObject<List<MyType>>(jsondata);
foreach (var item in items)
{
    string id = item.id;
    string name = item.fname;
}


这篇关于我如何循环json url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆