How to define a C# class model for these dataobjects [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How can I define classes with properties where the following code would compile and be valid?
AtomEntry newPost = new AtomEntry();
newPost.Title.Text = "Marriage!";
newPost.Content = new AtomContent();
newPost.Content.Content = "<div xmlns='http://www.w3.org/1999/xhtml'>" +
"<p>Mr. Darcy has <em>proposed marriage</em> to me!</p>" +
"<p>He is the last man on earth I would ever desire to marry.</p>" +
"<p>Whatever shall I do?</p>" +
"</div>";
newPost.Content.Type = "xhtml";

This should do it:
public class AtomEntry
{
public AtomContent Content { get; set; }
public Title Title { get; set; }
}
public class AtomContent
{
public string Content { get; set; }
public string Type { get; set; }
}
public class Title
{
public string Text { get; set; }
}

Related

How to parse complex json to c# .net classes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
my json is as given below. i need to convert it into c# class. Please note all values will be different in actual scenario.
{
'aa-AA': {
lanCODE: 'aa-AA',
genNames: {
female: ['Wavenet'],
male: ['Bavenet', 'Bavenet'],
},
default: 'Wavenet',
systemLocale: ['ara', 'aru', 'are', 'aro', 'arh', 'arm', 'arq', 'ark'],
name: 'xxxx',
},
'aa-AA': {
lanCODE: 'aa-AA',
genNames: {
female: ['Wavenet'],
male: ['Bavenet', 'Bavenet'],
},
default: 'Wavenet',
systemLocale: ['ara', 'aru', 'are', 'aro', 'arh', 'arm', 'arq', 'ark'],
name: 'xxxx',
},
'aa-AA': {
lanCODE: 'aa-AA',
genNames: {
female: ['Wavenet'],
male: ['Bavenet', 'Bavenet'],
},
default: 'Wavenet',
systemLocale: ['ara', 'aru', 'are', 'aro', 'arh', 'arm', 'arq', 'ark'],
name: 'xxxx',
}
}
The initial property is almost certainly meant to be a dictionary key, so I would go with something like this:
public class Language
{
[JsonProperty("lanCODE")]
public string LanguageCode { get; set; }
public string Default { get; set; }
public List<string> SystemLocale { get; set; }
public GenNames GenNames { get; set; }
}
public class GenNames
{
public List<string> Female { get; set; }
public List<string> Male { get; set; }
}
And deserialise like this:
var languages = JsonConvert.DeserializeObject<Dictionary<string, Language>>(json);

Parse nested values in C# dictionary [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
My C# application has the below json which is deserialized to a dictionary which is assigned to values:
{
"armSpan": 1.8081974983215332,
"handPosition": {
"x": 1.23,
"y": 1.74,
"z": 2.05,
}
}
This is the code which deserializes:
var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(response);
I want to assign data from it to various fields in my Size model. For armSpan I'm happy that the following works:
size.ArmSpan = decimal.Parse(values["armSpan"]);
I'm not sure how to get the values of x, y and z though. should it be something like
size.HandPosX = decimal.Parse(values["handPosition"]["x"]);
or
size.HandPosX = decimal.Parse(values["handPosition"].["x"]);
There are online converters to generate c# code based on your json (search for "JSON to C#"). With one of those, I made these classes based on the json you supplied (removed the extra comma in '"z": 2.05,'):
public partial class ClassYouDeserializeTo
{
[JsonProperty("armSpan")]
public double ArmSpan { get; set; }
[JsonProperty("handPosition")]
public HandPosition HandPosition { get; set; }
}
public partial class HandPosition
{
[JsonProperty("x")]
public double X { get; set; }
[JsonProperty("y")]
public double Y { get; set; }
[JsonProperty("z")]
public double Z { get; set; }
}
You can use them like this:
var values = JsonConvert.DeserializeObject<ClassYouDeserializeTo>(response);

How to load a property value of an object from database [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to pass a list of string to dropdownlist in my view. when I am passing this list as hard coded I am getting values in dropdownlist but I want to pass this list of strings from database. Following is my model class.
public class League
{
public int Id { get; set; }
public string LeagueName { get; set; }
public string Icon { get; set; }
}
and action is as
public ActionResult Index()
{
var leagues= new List<string> { "STAR", "GOLD", "SILVER" };
var leagueOptions = new SelectList(leagues);
ViewBag.leagues = leagueOptions;
return View();
}
till now everything is fine but I want to build a List<LeagueName> leagues = new LeagueName(); from my model class but do not know hot to query this.

Why complex types don't allow to reference entity types [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I do not understand why complex types do not allow to refrence entity types. I mean they do, but all properties of the refrenced entity type are then stored in the same table as the class including the complex types.
I do not see any reason for this and it is a great limitation.
It seems Entity Framework cannot distinguish between the referenced entities. For example, it is unable to automatically assign keys to them:
The database in the previous image was created using the following code:
static void CreateAndSeedDatabase()
{
Context context = new Context();
ReferencedClass anotherClass1 = new ReferencedClass(){Name="instance1"};
ReferencedClass anotherClass2 = new ReferencedClass() { Name = "instance2" };
ComplexTypeClass complexType1 = new ComplexTypeClass(){ReferencedClassProp = anotherClass1};
ComplexTypeClass complexType2 = new ComplexTypeClass() { ReferencedClassProp = anotherClass2 };
Parent parent1 = new Parent() { ComplexTypeClassProp = complexType1 };
Parent parent2 = new Parent() { ComplexTypeClassProp = complexType2 };
context.Parents.Add(parent1);
context.Parents.Add(parent2);
context.SaveChanges();
}
public class Context : DbContext
{
public Context()
{
Database.SetInitializer<Context>(new DropCreateDatabaseAlways<Context>());
Database.Initialize(true);
}
public DbSet<Parent> Parents { get; set; }
}
public class Parent
{
public int Id { get; set; }
public ComplexTypeClass ComplexTypeClassProp { get; set; }
}
[ComplexType]
public class ComplexTypeClass
{
public ReferencedClass ReferencedClassProp { get; set; }
}
public class ReferencedClass
{
public int Id { get; set; }
public string Name { get; set; }
}

Create JSON array in C# with asp.net [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How to create a JSON-Array in C# so that we can insert a variable (array of emails) for example "test#gmail;test1#gmail..." into it?
[
{
"To":[
{
"EntryType":2,
"username":"jack",
"email":"test#gmail.com"
}
],
"Cc":[
{
"EntryType":2,
"username":"mikle",
"email":"test1#gmail.com"
},
{
"EntryType":2,
"username":"jone",
"email":"test2#gmail.com"
}
],
"Bcc":[
{
"EntryType":2,
"username":"luis",
"email":"test3#gmail.com"
}
]
}
]
I used json2csharp.com to generated C# classes from your JSON. That results in this:
public class Recepient
{
public int EntryType { get; set; }
public string username { get; set; }
public string email { get; set; }
}
public class Mail
{
public List<Recepient> To { get; set; }
public List<Recepient> Cc { get; set; }
public List<Recepient> Bcc { get; set; }
}
Actually it created four other classes RootObject, To, Cc and Bcc but I renamed them to Mail and Recipient.
To create JSON from these classes, with Newtonsoft.Json, you can do this:
using Newtonsoft.Json;
public string Demo()
{
var mails = new List<Mail>();
var mail = new Mail();
mail.To = new List<Recepient>{
new Recepient
{
EntryType = 2,
username = "jack",
email = "test#gmail.com"
}
};
mail.Cc = new List<Recepient>();
mail.Bcc = new List<Recepient>();
mails.Add(mail);
return JsonConvert.SerializeObject(mails);
}
Use JSON.net and you will never look back.
http://www.newtonsoft.com/json/help/html/CreatingLINQtoJSON.htm

Categories