Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
detailsList = JsonConvert.DeserializeObject<list>
>(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);
}
Posted
Updated 26-May-16 0:45am
Comments
Karthik_Mahalingam 26-May-16 4:17am    
what is the data you are getting in json1 ?
Member 12547339 26-May-16 4:18am    
Json :

[
{
"id": 1,
"fname": "Jeff",
},
{
"id": 1,
"fname": "Jan",
},
{
"id": 1,
"fname": "Piet",
}
]
Karthik_Mahalingam 26-May-16 4:19am    
ok fine, so what you need from this data?
Member 12547339 26-May-16 4:25am    
I want to show all the 3 names on my form. Right now it only shows up the first "Jeff"
Member 12547339 26-May-16 4:42am    
?

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

C#
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.
 
Share this answer
 
Comments
Member 12547339 26-May-16 7:15am    
Thanks dude ! it worked. Mind if you can help me out getting the buttons next to the users?

for (int i = 0; i < detailsList; i++)
{

Button button = new Button();
button.Location = new Point(200, );
button.Text = "Off";
button.Tag = i;
button.BackColor = Color.Red;
this.Controls.Add(button);

button.Click += button_Click;
}

this is what I have now.
F-ES Sitecore 26-May-16 7:40am    
It's the same concept...note the x and y the label is at and give the Button control a Location also where the location is relative to the label.
try this using Json.NET - Newtonsoft[^]

Create a class with properties matching your json string
C#
class MyType
       {
           public string id { get; set; }
           public string fname { get; set; }
       }

and using Deserialise method can convert it to its list of Type
C#
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;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900