public class information
{
public string name { get; set; }
public string surname { get; set; }
}
public class JsonFormat
{
public IList<information> information { get; set; }
}
protected void gettextbox_Click(object sender, EventArgs e)
{
JsonFormat newjson = new JsonFormat();
List<information> p = new List<information>();
information add1 = new information { name = textbox1.Text , surname = textbox2.Text };
information add2 = new information { name = textbox3.Text, surname = textbox4.Text };
p.Add(add1);
p.Add(add2);
newjson.information = p;
string json = JsonConvert.SerializeObject(newjson);
}
string json here :
"{\"information\":[{\"name\":\"data1\",\"surname\":\"data2\"}, \"name\":\"data3\",\"surname\":\"data4\"}]}"
it's okay here but, how can I deserialize the data on the list?
Thank you in advance for your help.
List<information> informationList = JsonConvert.DeserializeObject<List<information>>(json);
https://www.newtonsoft.com/json/help/html/DeserializeObject.htm
Related
Im trying to access and display the value of a dictionary where the dictionary has no real name but is a property of a class.
Currently I have an enum "Roles" with three elements (fighter, rogue, and sorcerer), and:
public class Adventurer
{
public int ID { get; set; }
public string Name { get; set; }
public Roles Role { get; set; }
public List<Skill> Skills { get; set; }
public override string ToString()
{
return $"{ID}" + " - " + $"{Name}" + " - " + $"{Role}";
}
}
and:
public class Skill
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Dictionary<Roles, Skill> LinkedTo { get; set; }
}
and in another class I have:
private void CreateSkills()
{
Skill swordFighting = new Skill() { ID = 1, Name = "Sword fighting"};
Skill stealth = new Skill() { ID = 2, Name = "Stealth"};
Skill magic = new Skill() { ID = 3, Name = "Magic"};
swordFighting.LinkedTo = new Dictionary<Roles, Skill>
{
{ Roles.Fighter, swordFighting }
};
stealth.LinkedTo = new Dictionary<Roles, Skill>
{
{ Roles.Rogue, stealth }
};
magic.LinkedTo = new Dictionary<Roles, Skill>
{
{ Roles.Sorcerer, magic }
};
}
private void DisplaySkills(Adventurer adventurer)
{
adventurer.Skills = adventurer.Role.LinkedTo; // I WOULD LIKE SOMETHING LIKE THIS...
lstAdventurer.ItemsSource = adventurer.Skills;
}
Is there some way of accessing the values (skills) of the adventurer by knowing only the role (fighter/rogue/sorcerer)?
Best,
Dedoj
Would you mean something like this?
for known Roles like Roles.Fighter:
adventurer.Skills = adventurer.Skills
.Select(s => s.LinkedTo.ContainsKey(Roles.Fighter) ? s.LinkedTo[Roles.Fighter] : null)
.Where(s => s != null).ToList();
How can I send a data which is saved in a class:
public class Person
{
public int Id { get; set; }
public string Fullname { get; set; }
public string Details { get; set; }
public string UserType { get; set; }
}
to the variable?
I'm saving data (in another class) like this:
var persons = new List<Person>();
while (dr1.Read())
{
// get the results of each column
int id = (int)dr1["ID_Instructor"];
string firstname = (string)dr1["f_name"];
string lastname = (string)dr1["l_name"];
string school = (string)dr1["d_school"];
string category = (string)dr1["category"];
var person = new Person
{
Id = id,
Fullname = firstname + " " + lastname,
Details = school + " " + category
};
persons.Add(person);
}
and in the same class I want to send it to the variable from menuItem by the BindingContext. I just need to print all of this data (from one person from list)
private void MenuItem_Clicked_1(object sender, EventArgs e)
{
var menuItem = sender as MenuItem;
if (menuItem != null)
var name = menuItem.BindingContex;
private void Submit_Click(object sender, EventArgs e)
{
ScoutContext db = new ScoutContext();
ScoutData cust = new ScoutData();
cust.FName = textBox1.Text;
cust.LName = textBox2.Text;
cust.FName = textBox3.Text;
cust.FaWork = textBox4.Text;
cust.MoName = textBox5.Text;
cust.MaWork = textBox6.Text;
cust.PlaceOfBirth = textBox7.Text;
cust.City = textBox8.Text;
cust.School = textBox9.Text;
cust.FaceBook = textBox10.Text;
cust.Phone = textBox11.Text;
cust.MPhone = textBox12.Text;
cust.IDNumber = textBox13.Text;
cust.NOfQaid = textBox14.Text;
cust.GroupID = ?????????????????
db.SaveChanges();
}
i work on Windows form, i have this data that the user fill the textbox, after that i need to save the data to my context ( database ), this is my code to insert data to my data base , but i have data ( numbers and some string ) the user will chose from ComboBox. i need to get this data and save it to a list of object , this is the code :
public class Groups
{
[Key]
public string GroupsID { set; get; }
public string NameOfGroup { set; get; }
***public virtual List<ScoutData> Members { set; get; }***
}
The context:
public class ScoutContext : DbContext
{
public ScoutContext()
: base("Scout")
{
// if (!Database.Exists("ScoutData"))
// Database.SetInitializer(new DropCreateDatabaseAlways<ScoutContext>());
}
public DbSet<ScoutData> ScoutDatas { set; get; }
public DbSet<Groups> GroupesScout { set; get; }
}
i need to get this data from the combobox to Members list and save it to a list of object (Members)
It depends on what you have in the combobox.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var combo = sender as ComboBox;
// If combobox has ScoutData then do this
var item = combo.SelectedItem as ScoutData;
// If combobox has something else then do this
var item2 = combo.SelectedItem as SomeThingElse;
var newScout = new ScoutData { FName = item2.FName /*, etc, etc */ };
// Then add it to your list
}
This is my code in adding Fulfillment to Shopify orders but the converted json is not as expected.
Fullfillment product = new Fullfillment();
product.status = "success";
product.tracking_number = orderSent.TrackingNo;
List<LineItems> items = new List<LineItems>();
foreach (var item in orderSent.OrderLines)
{
LineItems line = new LineItems();
line.id = item.ProductName;
items.Add(line);
}
var json = JsonConvert.SerializeObject(product);
json = "{ \"fulfillment\": " + json + "}";
var json1 = JsonConvert.SerializeObject(items);
json = json + "{ \"line_items\": " + json1 + "}";
And this the converted json from this code:
{ "fulfillment": {
"id":0,
"status":"success",
"tracking_number":"xxxx12222",
}}{
"line_items": [
{
"id":"1234566645"
}
]
}
How can I turned like this:
{
"fulfillment": {
"tracking_number": null,
"line_items": [
{
"id": 466157049,
"quantity": 1
}
]
}
}
Model:
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class Fullfillment
{
[JsonProperty(PropertyName = "id")]
public long id { get; set; }
[JsonProperty(PropertyName = "status")]
public string status { get; set; }
[JsonProperty(PropertyName = "tracking_number")]
public string tracking_number { get; set; }
}
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class LineItems
{
[JsonProperty(PropertyName = "id")]
public string id { get; set; }
}
These are the models for Fulfillment and Line Items.
Thank you in advance for giving advices and help.
This works for me:
var json = JsonConvert.SerializeObject(new
{
fullfillment = new
{
product.tracking_number,
line_items = items.Select(x => new
{
x.id,
quantity = 1
})
}
});
That gives me:
{
"fullfillment" : {
"tracking_number" : "xxxx12222",
"line_items" : [{
"id" : "1234566645",
"quantity" : 1
}
]
}
}
I started with this code to build up the JSON above:
Fullfillment product = new Fullfillment();
product.status = "success";
product.tracking_number = "xxxx12222";
List<LineItems> items = new List<LineItems>();
LineItems line = new LineItems();
line.id = "1234566645";
items.Add(line);
Obviously you need to fill in your specific data.
Change your classes like below.
public class Rootobject
{
public Fulfillment fulfillment { get; set; }
}
public class Fulfillment
{
public string tracking_number { get; set; }
public Line_Items[] line_items { get; set; }
}
public class Line_Items
{
public string id { get; set; }
public int quantity { get; set; }
}
public class JsonTest
{
public void Test()
{
var root = new Rootobject();
root.fulfillment = new Fulfillment();
root.fulfillment.tracking_number = "xxxx12222";
root.fulfillment.line_items = new List<Line_Items>() { new Line_Items() { id = "1234566645", quantity = 1 } }.ToArray();
var json = JsonConvert.SerializeObject(root);
Console.WriteLine(json);
}
}
This will give you this json.
{
"fulfillment": {
"tracking_number": "xxxx12222",
"line_items": [
{
"id": "1234566645",
"quantity": 1
}
]
}
}
Try the following
public class Rootobject
{
public Fulfillment fulfillment { get; set; }
}
public class Fulfillment
{
public string tracking_number { get; set; }
public Line_Items[] line_items { get; set; }
}
public class Line_Items
{
public string id { get; set; }
public int quantity { get; set; }
}
public class JsonTest
{
public void Test()
{
var root = new Rootobject();
root.fulfillment = new Fulfillment();
root.fulfillment.tracking_number = "xxxx12222";
root.fulfillment.line_items = new List<Line_Items>() { new Line_Items() { id = "1234566645", quantity = 1 } }.ToArray();
var json = JsonConvert.SerializeObject(root);
Console.WriteLine(json);
}
}
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",
}
]
Problem is code is updating the same label again and again.
Try creating new Label each detail.
FlowLayoutPanel flowLayoutPanel1 = new FlowLayoutPanel();
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
flowLayoutPanel1.WrapContents = false;
flowLayoutPanel1.AutoScroll = true;
this.Controls.Add(flowLayoutPanel1);
foreach (details dets1 in detailsList)
{
var label = new Label();
label.Name = dets1.fname;
label.Text = dets1.fname;
flowLayoutPanel1.Controls.Add(label);
}