Convert JSON ToList and Parse it to EF Model





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I'm trying to convert the json file I have ToList in C# after read it and then pass it to my model and add it to db. Now, my question is: how I can do that?



Here is my Json:



    {
"Course: 1": {
"Name": "Fundamentals of Yoga",
"Category": "Health & Fitness",
"Headline": "An all levels guide to your yoga practice.",
"CreatedBy": "Carleen Rose",
"ExternalImage": "https://udemy-images.udemy.com/course/750x422/1854410_de8b.jpg",
"Duration": "5 hours",
"Description": {
"What You'll Learn": [
"You'll have a great understanding of the foundations of yoga and basic anatomy so you'll safely be able to practice yoga anywhere!",
"You'll be comfortable practicing in a public class and confident enough to practice yoga at home."
],
"Requirements": [
"The desire to begin or deepen your yoga practice!",
"A place to practice, preferably on a yoga mat."
],
"Description": "<div>n Descriptionn </div>nn <p>Imagine building a house with the roof first. Or growing a tree starting with it's branches. Crazy right?&nbsp; Yet that's how many people start their yoga practice, with the imagined "finished product" they're trying to achieve. Even for a practiced yogi this can result in not knowing what you're doing or getting hurt.&nbsp; So many classes are still taught u201cpose firstu201d but this revolutionary class is designed foundation first.</p><p>This course will start your practice from it's foundation or roots. It uses anatomy and physics to build your poses in a healthy alignment for your body and optimize your practice. It also activates your deep core strength, heats up your body and transitions you through poses in a way that gets you more results in less time and makes transformation accessible on all levels.</p><p>Whether you're looking for beginner yoga or something more advanced, this course will help you take your yoga practice to the next level.</p><p>Ready to channel your inner-superhero and transform your yoga practice? Then I'll see you on the mat! Namaste!!</p><p><strong>This course includes:</strong></p><ul><li><p>Workshop style videos teaching the foundations of yoga asana and anatomy</p></li><li><p>Topics and philosophies not normally discussed in a yoga class</p></li><li><p>Variety of complete yoga flows for your home practice</p></li><li><p>Pose tutorials of fun and challenging yoga poses</p></li><li><p>Surprise bonus material!</p></li></ul>nn n <div class="audience" data-purpose="course-audience">n <div class="audience__title">n Who is the target audience?n </div>n <ul class="n n <li>Anyone who wants to build a lifelong yoga practice and enjoy all the benefits of yoga.</li>n n <li>Any level yogi, especially if you're wanting to take your practice outside of the yoga studio.</li>n n <li>Beginner yogis who want to learn the basics of yoga and start out their yoga journey.</li>n n <li>Advanced yogis who want to expand their practice and take it to the next level.</li>n n </ul>"
}
},
"Course: 2": {
"Name": "Complete Forex Trading- At Price Action Tricks and Tips",
"Category": "Business",
"Headline": "Recognise Market Trend And Start Interday or long term trading without taking big risk and get huge profit",
"CreatedBy": "Rizwan azim",
"ExternalImage": "https://udemy-images.udemy.com/course/750x422/2023746_5bba_2.jpg",
"Duration": "1 hour",
"Description": {
"What You'll Learn": [
"Become professional trader and can earn good profit in forex buisness"
],
"Requirements": [
"Just Have Pc and Internet Connection"
],
"Description": "<div>n Descriptionn </div>nn <p>ake this course now and learn from my Long Time experience.</p><p> Avoid the most common pitfalls that catch 90% of traders!</p><p>Everything I explain here at live Mt4 Chart step by step </p><p>by live examples</p><p>Recognise Market Trend And Start Interday or long term trading</p><p>without taking big risk and get huge profit from every Trade </p><p><br></p><p> All you need is an open mind and a passion to be successful!</p><p><br></p><p>you have unlimited lifetime access at no extra costs, </p><p>ever</p><p>all future additional lectures, bonuses, etc</p><p><br></p>nn n <div class="audience" data-purpose="course-audience">n <div class="audience__title">n Who is the target audience?n </div>n <ul class="n n <li>Every business mind person who want to earn big profit in Short time</li>n n </ul>"
}
}
}


My Post Model Class:



[Table("Post")]

public partial class Post
{
[Key]
public int Id { get; set; }

[Required]
[Display(Name = "Title")]
[StringLength(150)]
public string Name { get; set; }

[AllowHtml]
[Display(Name = "Description")]
[Column(TypeName = "text")]
public string Description { get; set; }

[Display(Name = "External Image")]
[Required]
[StringLength(200)]
public string ExternalImage { get; set; }

[Display(Name = "Author")]
[StringLength(150)]
public string CreatedBy { get; set; }

[Display(Name = "Size")]
[StringLength(150)]
public string Size { get; set; }

[Display(Name = "Category")]
public int? Category_id { get; set; }

[Display(Name = "Date")]
public DateTime PublishDate { get; set; }

[Display(Name = "Duration")]
[StringLength(150)]
public string Duration { get; set; }


}


and here is my code :



using (StreamReader r = new StreamReader("/data.json"))
{
string json = r.ReadToEnd();
dynamic array = JsonConvert.DeserializeObject(json);
foreach (var item in array)
{
var ii = item;
}

}


What I need to do simply:




  1. Convert the Json data => Tolist()

  2. Read it from foreach loop

  3. Assign the Values to the model and save it to database.










share|improve this question




















  • 1





    Well, there's nothing about a database in your code example, which bit are you struggling with? Can you map the JSON to a real class rather than dynamic? I mean, your JSON isn't actually an array so you won't be able to for each over it.

    – Jamie Twells
    Nov 28 '18 at 12:21













  • Thanks for replying to me, Actually why i should mention db ? already i posted my model class and what i need is to read from foreach and pass to db .. Anyway i have no problem to create a new class and save the values there then assign them to my model .. but how i can read the json from foreach ? that what i have no idea how to do

    – Dummies EBooks
    Nov 28 '18 at 12:28











  • I think, if you're struggling to save to a database then you should ask another question and use this question to ask about parsing the JSON. If you need to iterate over the JSON courses you should read it in as a dictionary of string to some concrete class that represents the course.

    – Jamie Twells
    Nov 28 '18 at 12:35











  • I think already i explained what i do really need to do .. Any way i tried your suggestion and created a class and when i tried to fetch data it says i should change Json to Json Array!

    – Dummies EBooks
    Nov 28 '18 at 12:38


















1















I'm trying to convert the json file I have ToList in C# after read it and then pass it to my model and add it to db. Now, my question is: how I can do that?



Here is my Json:



    {
"Course: 1": {
"Name": "Fundamentals of Yoga",
"Category": "Health &amp; Fitness",
"Headline": "An all levels guide to your yoga practice.",
"CreatedBy": "Carleen Rose",
"ExternalImage": "https://udemy-images.udemy.com/course/750x422/1854410_de8b.jpg",
"Duration": "5 hours",
"Description": {
"What You'll Learn": [
"You'll have a great understanding of the foundations of yoga and basic anatomy so you'll safely be able to practice yoga anywhere!",
"You'll be comfortable practicing in a public class and confident enough to practice yoga at home."
],
"Requirements": [
"The desire to begin or deepen your yoga practice!",
"A place to practice, preferably on a yoga mat."
],
"Description": "<div>n Descriptionn </div>nn <p>Imagine building a house with the roof first. Or growing a tree starting with it's branches. Crazy right?&nbsp; Yet that's how many people start their yoga practice, with the imagined "finished product" they're trying to achieve. Even for a practiced yogi this can result in not knowing what you're doing or getting hurt.&nbsp; So many classes are still taught u201cpose firstu201d but this revolutionary class is designed foundation first.</p><p>This course will start your practice from it's foundation or roots. It uses anatomy and physics to build your poses in a healthy alignment for your body and optimize your practice. It also activates your deep core strength, heats up your body and transitions you through poses in a way that gets you more results in less time and makes transformation accessible on all levels.</p><p>Whether you're looking for beginner yoga or something more advanced, this course will help you take your yoga practice to the next level.</p><p>Ready to channel your inner-superhero and transform your yoga practice? Then I'll see you on the mat! Namaste!!</p><p><strong>This course includes:</strong></p><ul><li><p>Workshop style videos teaching the foundations of yoga asana and anatomy</p></li><li><p>Topics and philosophies not normally discussed in a yoga class</p></li><li><p>Variety of complete yoga flows for your home practice</p></li><li><p>Pose tutorials of fun and challenging yoga poses</p></li><li><p>Surprise bonus material!</p></li></ul>nn n <div class="audience" data-purpose="course-audience">n <div class="audience__title">n Who is the target audience?n </div>n <ul class="n n <li>Anyone who wants to build a lifelong yoga practice and enjoy all the benefits of yoga.</li>n n <li>Any level yogi, especially if you're wanting to take your practice outside of the yoga studio.</li>n n <li>Beginner yogis who want to learn the basics of yoga and start out their yoga journey.</li>n n <li>Advanced yogis who want to expand their practice and take it to the next level.</li>n n </ul>"
}
},
"Course: 2": {
"Name": "Complete Forex Trading- At Price Action Tricks and Tips",
"Category": "Business",
"Headline": "Recognise Market Trend And Start Interday or long term trading without taking big risk and get huge profit",
"CreatedBy": "Rizwan azim",
"ExternalImage": "https://udemy-images.udemy.com/course/750x422/2023746_5bba_2.jpg",
"Duration": "1 hour",
"Description": {
"What You'll Learn": [
"Become professional trader and can earn good profit in forex buisness"
],
"Requirements": [
"Just Have Pc and Internet Connection"
],
"Description": "<div>n Descriptionn </div>nn <p>ake this course now and learn from my Long Time experience.</p><p> Avoid the most common pitfalls that catch 90% of traders!</p><p>Everything I explain here at live Mt4 Chart step by step </p><p>by live examples</p><p>Recognise Market Trend And Start Interday or long term trading</p><p>without taking big risk and get huge profit from every Trade </p><p><br></p><p> All you need is an open mind and a passion to be successful!</p><p><br></p><p>you have unlimited lifetime access at no extra costs, </p><p>ever</p><p>all future additional lectures, bonuses, etc</p><p><br></p>nn n <div class="audience" data-purpose="course-audience">n <div class="audience__title">n Who is the target audience?n </div>n <ul class="n n <li>Every business mind person who want to earn big profit in Short time</li>n n </ul>"
}
}
}


My Post Model Class:



[Table("Post")]

public partial class Post
{
[Key]
public int Id { get; set; }

[Required]
[Display(Name = "Title")]
[StringLength(150)]
public string Name { get; set; }

[AllowHtml]
[Display(Name = "Description")]
[Column(TypeName = "text")]
public string Description { get; set; }

[Display(Name = "External Image")]
[Required]
[StringLength(200)]
public string ExternalImage { get; set; }

[Display(Name = "Author")]
[StringLength(150)]
public string CreatedBy { get; set; }

[Display(Name = "Size")]
[StringLength(150)]
public string Size { get; set; }

[Display(Name = "Category")]
public int? Category_id { get; set; }

[Display(Name = "Date")]
public DateTime PublishDate { get; set; }

[Display(Name = "Duration")]
[StringLength(150)]
public string Duration { get; set; }


}


and here is my code :



using (StreamReader r = new StreamReader("/data.json"))
{
string json = r.ReadToEnd();
dynamic array = JsonConvert.DeserializeObject(json);
foreach (var item in array)
{
var ii = item;
}

}


What I need to do simply:




  1. Convert the Json data => Tolist()

  2. Read it from foreach loop

  3. Assign the Values to the model and save it to database.










share|improve this question




















  • 1





    Well, there's nothing about a database in your code example, which bit are you struggling with? Can you map the JSON to a real class rather than dynamic? I mean, your JSON isn't actually an array so you won't be able to for each over it.

    – Jamie Twells
    Nov 28 '18 at 12:21













  • Thanks for replying to me, Actually why i should mention db ? already i posted my model class and what i need is to read from foreach and pass to db .. Anyway i have no problem to create a new class and save the values there then assign them to my model .. but how i can read the json from foreach ? that what i have no idea how to do

    – Dummies EBooks
    Nov 28 '18 at 12:28











  • I think, if you're struggling to save to a database then you should ask another question and use this question to ask about parsing the JSON. If you need to iterate over the JSON courses you should read it in as a dictionary of string to some concrete class that represents the course.

    – Jamie Twells
    Nov 28 '18 at 12:35











  • I think already i explained what i do really need to do .. Any way i tried your suggestion and created a class and when i tried to fetch data it says i should change Json to Json Array!

    – Dummies EBooks
    Nov 28 '18 at 12:38














1












1








1








I'm trying to convert the json file I have ToList in C# after read it and then pass it to my model and add it to db. Now, my question is: how I can do that?



Here is my Json:



    {
"Course: 1": {
"Name": "Fundamentals of Yoga",
"Category": "Health &amp; Fitness",
"Headline": "An all levels guide to your yoga practice.",
"CreatedBy": "Carleen Rose",
"ExternalImage": "https://udemy-images.udemy.com/course/750x422/1854410_de8b.jpg",
"Duration": "5 hours",
"Description": {
"What You'll Learn": [
"You'll have a great understanding of the foundations of yoga and basic anatomy so you'll safely be able to practice yoga anywhere!",
"You'll be comfortable practicing in a public class and confident enough to practice yoga at home."
],
"Requirements": [
"The desire to begin or deepen your yoga practice!",
"A place to practice, preferably on a yoga mat."
],
"Description": "<div>n Descriptionn </div>nn <p>Imagine building a house with the roof first. Or growing a tree starting with it's branches. Crazy right?&nbsp; Yet that's how many people start their yoga practice, with the imagined "finished product" they're trying to achieve. Even for a practiced yogi this can result in not knowing what you're doing or getting hurt.&nbsp; So many classes are still taught u201cpose firstu201d but this revolutionary class is designed foundation first.</p><p>This course will start your practice from it's foundation or roots. It uses anatomy and physics to build your poses in a healthy alignment for your body and optimize your practice. It also activates your deep core strength, heats up your body and transitions you through poses in a way that gets you more results in less time and makes transformation accessible on all levels.</p><p>Whether you're looking for beginner yoga or something more advanced, this course will help you take your yoga practice to the next level.</p><p>Ready to channel your inner-superhero and transform your yoga practice? Then I'll see you on the mat! Namaste!!</p><p><strong>This course includes:</strong></p><ul><li><p>Workshop style videos teaching the foundations of yoga asana and anatomy</p></li><li><p>Topics and philosophies not normally discussed in a yoga class</p></li><li><p>Variety of complete yoga flows for your home practice</p></li><li><p>Pose tutorials of fun and challenging yoga poses</p></li><li><p>Surprise bonus material!</p></li></ul>nn n <div class="audience" data-purpose="course-audience">n <div class="audience__title">n Who is the target audience?n </div>n <ul class="n n <li>Anyone who wants to build a lifelong yoga practice and enjoy all the benefits of yoga.</li>n n <li>Any level yogi, especially if you're wanting to take your practice outside of the yoga studio.</li>n n <li>Beginner yogis who want to learn the basics of yoga and start out their yoga journey.</li>n n <li>Advanced yogis who want to expand their practice and take it to the next level.</li>n n </ul>"
}
},
"Course: 2": {
"Name": "Complete Forex Trading- At Price Action Tricks and Tips",
"Category": "Business",
"Headline": "Recognise Market Trend And Start Interday or long term trading without taking big risk and get huge profit",
"CreatedBy": "Rizwan azim",
"ExternalImage": "https://udemy-images.udemy.com/course/750x422/2023746_5bba_2.jpg",
"Duration": "1 hour",
"Description": {
"What You'll Learn": [
"Become professional trader and can earn good profit in forex buisness"
],
"Requirements": [
"Just Have Pc and Internet Connection"
],
"Description": "<div>n Descriptionn </div>nn <p>ake this course now and learn from my Long Time experience.</p><p> Avoid the most common pitfalls that catch 90% of traders!</p><p>Everything I explain here at live Mt4 Chart step by step </p><p>by live examples</p><p>Recognise Market Trend And Start Interday or long term trading</p><p>without taking big risk and get huge profit from every Trade </p><p><br></p><p> All you need is an open mind and a passion to be successful!</p><p><br></p><p>you have unlimited lifetime access at no extra costs, </p><p>ever</p><p>all future additional lectures, bonuses, etc</p><p><br></p>nn n <div class="audience" data-purpose="course-audience">n <div class="audience__title">n Who is the target audience?n </div>n <ul class="n n <li>Every business mind person who want to earn big profit in Short time</li>n n </ul>"
}
}
}


My Post Model Class:



[Table("Post")]

public partial class Post
{
[Key]
public int Id { get; set; }

[Required]
[Display(Name = "Title")]
[StringLength(150)]
public string Name { get; set; }

[AllowHtml]
[Display(Name = "Description")]
[Column(TypeName = "text")]
public string Description { get; set; }

[Display(Name = "External Image")]
[Required]
[StringLength(200)]
public string ExternalImage { get; set; }

[Display(Name = "Author")]
[StringLength(150)]
public string CreatedBy { get; set; }

[Display(Name = "Size")]
[StringLength(150)]
public string Size { get; set; }

[Display(Name = "Category")]
public int? Category_id { get; set; }

[Display(Name = "Date")]
public DateTime PublishDate { get; set; }

[Display(Name = "Duration")]
[StringLength(150)]
public string Duration { get; set; }


}


and here is my code :



using (StreamReader r = new StreamReader("/data.json"))
{
string json = r.ReadToEnd();
dynamic array = JsonConvert.DeserializeObject(json);
foreach (var item in array)
{
var ii = item;
}

}


What I need to do simply:




  1. Convert the Json data => Tolist()

  2. Read it from foreach loop

  3. Assign the Values to the model and save it to database.










share|improve this question
















I'm trying to convert the json file I have ToList in C# after read it and then pass it to my model and add it to db. Now, my question is: how I can do that?



Here is my Json:



    {
"Course: 1": {
"Name": "Fundamentals of Yoga",
"Category": "Health &amp; Fitness",
"Headline": "An all levels guide to your yoga practice.",
"CreatedBy": "Carleen Rose",
"ExternalImage": "https://udemy-images.udemy.com/course/750x422/1854410_de8b.jpg",
"Duration": "5 hours",
"Description": {
"What You'll Learn": [
"You'll have a great understanding of the foundations of yoga and basic anatomy so you'll safely be able to practice yoga anywhere!",
"You'll be comfortable practicing in a public class and confident enough to practice yoga at home."
],
"Requirements": [
"The desire to begin or deepen your yoga practice!",
"A place to practice, preferably on a yoga mat."
],
"Description": "<div>n Descriptionn </div>nn <p>Imagine building a house with the roof first. Or growing a tree starting with it's branches. Crazy right?&nbsp; Yet that's how many people start their yoga practice, with the imagined "finished product" they're trying to achieve. Even for a practiced yogi this can result in not knowing what you're doing or getting hurt.&nbsp; So many classes are still taught u201cpose firstu201d but this revolutionary class is designed foundation first.</p><p>This course will start your practice from it's foundation or roots. It uses anatomy and physics to build your poses in a healthy alignment for your body and optimize your practice. It also activates your deep core strength, heats up your body and transitions you through poses in a way that gets you more results in less time and makes transformation accessible on all levels.</p><p>Whether you're looking for beginner yoga or something more advanced, this course will help you take your yoga practice to the next level.</p><p>Ready to channel your inner-superhero and transform your yoga practice? Then I'll see you on the mat! Namaste!!</p><p><strong>This course includes:</strong></p><ul><li><p>Workshop style videos teaching the foundations of yoga asana and anatomy</p></li><li><p>Topics and philosophies not normally discussed in a yoga class</p></li><li><p>Variety of complete yoga flows for your home practice</p></li><li><p>Pose tutorials of fun and challenging yoga poses</p></li><li><p>Surprise bonus material!</p></li></ul>nn n <div class="audience" data-purpose="course-audience">n <div class="audience__title">n Who is the target audience?n </div>n <ul class="n n <li>Anyone who wants to build a lifelong yoga practice and enjoy all the benefits of yoga.</li>n n <li>Any level yogi, especially if you're wanting to take your practice outside of the yoga studio.</li>n n <li>Beginner yogis who want to learn the basics of yoga and start out their yoga journey.</li>n n <li>Advanced yogis who want to expand their practice and take it to the next level.</li>n n </ul>"
}
},
"Course: 2": {
"Name": "Complete Forex Trading- At Price Action Tricks and Tips",
"Category": "Business",
"Headline": "Recognise Market Trend And Start Interday or long term trading without taking big risk and get huge profit",
"CreatedBy": "Rizwan azim",
"ExternalImage": "https://udemy-images.udemy.com/course/750x422/2023746_5bba_2.jpg",
"Duration": "1 hour",
"Description": {
"What You'll Learn": [
"Become professional trader and can earn good profit in forex buisness"
],
"Requirements": [
"Just Have Pc and Internet Connection"
],
"Description": "<div>n Descriptionn </div>nn <p>ake this course now and learn from my Long Time experience.</p><p> Avoid the most common pitfalls that catch 90% of traders!</p><p>Everything I explain here at live Mt4 Chart step by step </p><p>by live examples</p><p>Recognise Market Trend And Start Interday or long term trading</p><p>without taking big risk and get huge profit from every Trade </p><p><br></p><p> All you need is an open mind and a passion to be successful!</p><p><br></p><p>you have unlimited lifetime access at no extra costs, </p><p>ever</p><p>all future additional lectures, bonuses, etc</p><p><br></p>nn n <div class="audience" data-purpose="course-audience">n <div class="audience__title">n Who is the target audience?n </div>n <ul class="n n <li>Every business mind person who want to earn big profit in Short time</li>n n </ul>"
}
}
}


My Post Model Class:



[Table("Post")]

public partial class Post
{
[Key]
public int Id { get; set; }

[Required]
[Display(Name = "Title")]
[StringLength(150)]
public string Name { get; set; }

[AllowHtml]
[Display(Name = "Description")]
[Column(TypeName = "text")]
public string Description { get; set; }

[Display(Name = "External Image")]
[Required]
[StringLength(200)]
public string ExternalImage { get; set; }

[Display(Name = "Author")]
[StringLength(150)]
public string CreatedBy { get; set; }

[Display(Name = "Size")]
[StringLength(150)]
public string Size { get; set; }

[Display(Name = "Category")]
public int? Category_id { get; set; }

[Display(Name = "Date")]
public DateTime PublishDate { get; set; }

[Display(Name = "Duration")]
[StringLength(150)]
public string Duration { get; set; }


}


and here is my code :



using (StreamReader r = new StreamReader("/data.json"))
{
string json = r.ReadToEnd();
dynamic array = JsonConvert.DeserializeObject(json);
foreach (var item in array)
{
var ii = item;
}

}


What I need to do simply:




  1. Convert the Json data => Tolist()

  2. Read it from foreach loop

  3. Assign the Values to the model and save it to database.







c# json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '18 at 1:20









Tetsuya Yamamoto

17.1k42342




17.1k42342










asked Nov 28 '18 at 12:09









Dummies EBooksDummies EBooks

837




837








  • 1





    Well, there's nothing about a database in your code example, which bit are you struggling with? Can you map the JSON to a real class rather than dynamic? I mean, your JSON isn't actually an array so you won't be able to for each over it.

    – Jamie Twells
    Nov 28 '18 at 12:21













  • Thanks for replying to me, Actually why i should mention db ? already i posted my model class and what i need is to read from foreach and pass to db .. Anyway i have no problem to create a new class and save the values there then assign them to my model .. but how i can read the json from foreach ? that what i have no idea how to do

    – Dummies EBooks
    Nov 28 '18 at 12:28











  • I think, if you're struggling to save to a database then you should ask another question and use this question to ask about parsing the JSON. If you need to iterate over the JSON courses you should read it in as a dictionary of string to some concrete class that represents the course.

    – Jamie Twells
    Nov 28 '18 at 12:35











  • I think already i explained what i do really need to do .. Any way i tried your suggestion and created a class and when i tried to fetch data it says i should change Json to Json Array!

    – Dummies EBooks
    Nov 28 '18 at 12:38














  • 1





    Well, there's nothing about a database in your code example, which bit are you struggling with? Can you map the JSON to a real class rather than dynamic? I mean, your JSON isn't actually an array so you won't be able to for each over it.

    – Jamie Twells
    Nov 28 '18 at 12:21













  • Thanks for replying to me, Actually why i should mention db ? already i posted my model class and what i need is to read from foreach and pass to db .. Anyway i have no problem to create a new class and save the values there then assign them to my model .. but how i can read the json from foreach ? that what i have no idea how to do

    – Dummies EBooks
    Nov 28 '18 at 12:28











  • I think, if you're struggling to save to a database then you should ask another question and use this question to ask about parsing the JSON. If you need to iterate over the JSON courses you should read it in as a dictionary of string to some concrete class that represents the course.

    – Jamie Twells
    Nov 28 '18 at 12:35











  • I think already i explained what i do really need to do .. Any way i tried your suggestion and created a class and when i tried to fetch data it says i should change Json to Json Array!

    – Dummies EBooks
    Nov 28 '18 at 12:38








1




1





Well, there's nothing about a database in your code example, which bit are you struggling with? Can you map the JSON to a real class rather than dynamic? I mean, your JSON isn't actually an array so you won't be able to for each over it.

– Jamie Twells
Nov 28 '18 at 12:21







Well, there's nothing about a database in your code example, which bit are you struggling with? Can you map the JSON to a real class rather than dynamic? I mean, your JSON isn't actually an array so you won't be able to for each over it.

– Jamie Twells
Nov 28 '18 at 12:21















Thanks for replying to me, Actually why i should mention db ? already i posted my model class and what i need is to read from foreach and pass to db .. Anyway i have no problem to create a new class and save the values there then assign them to my model .. but how i can read the json from foreach ? that what i have no idea how to do

– Dummies EBooks
Nov 28 '18 at 12:28





Thanks for replying to me, Actually why i should mention db ? already i posted my model class and what i need is to read from foreach and pass to db .. Anyway i have no problem to create a new class and save the values there then assign them to my model .. but how i can read the json from foreach ? that what i have no idea how to do

– Dummies EBooks
Nov 28 '18 at 12:28













I think, if you're struggling to save to a database then you should ask another question and use this question to ask about parsing the JSON. If you need to iterate over the JSON courses you should read it in as a dictionary of string to some concrete class that represents the course.

– Jamie Twells
Nov 28 '18 at 12:35





I think, if you're struggling to save to a database then you should ask another question and use this question to ask about parsing the JSON. If you need to iterate over the JSON courses you should read it in as a dictionary of string to some concrete class that represents the course.

– Jamie Twells
Nov 28 '18 at 12:35













I think already i explained what i do really need to do .. Any way i tried your suggestion and created a class and when i tried to fetch data it says i should change Json to Json Array!

– Dummies EBooks
Nov 28 '18 at 12:38





I think already i explained what i do really need to do .. Any way i tried your suggestion and created a class and when i tried to fetch data it says i should change Json to Json Array!

– Dummies EBooks
Nov 28 '18 at 12:38












1 Answer
1






active

oldest

votes


















3














I would suggest first to make some classes to model the JSON:



public class Course
{
public string Name { get; set; }
public string Category { get; set; }
public string Headline { get; set; }
public string CreatedBy { get; set; }
public string ExternalImage { get; set; }
public string Duration { get; set; }
public Description Description { get; set; }
}

public class Description
{
[JsonProperty("What You'll Learn")]
public List<string> WhatYoullLearn { get; set; }
public List<string> Requirements { get; set; }
[JsonProperty("Description")]
public string DescriptionHtml { get; set; }
}


Then, I would suggest deserialising into the class, as follows:



var json = File.ReadAllText("/data.json");
var courses = JsonConvert.DeserializeObject<Dictionary<string, Course>>(json);
foreach (var course in courses)
{
var courseName = course.Key;
var courseDetails = course.Value;
// Do something with the data, e.g save it to your database
}





share|improve this answer
























  • Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,Tutorial.ViewModel.js]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

    – Dummies EBooks
    Nov 28 '18 at 13:15











  • It's works , i just removed the [ ] between the start and end .. thanks for helping me and i appreciate that

    – Dummies EBooks
    Nov 28 '18 at 13:18











  • @Dummies EBooks Good. I tested with the JSON you provided in the question and it worked for me so not sure which you needed to remove, but I'm glad it worked anyway.

    – Jamie Twells
    Nov 28 '18 at 13:29












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53519148%2fconvert-json-tolist-and-parse-it-to-ef-model%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














I would suggest first to make some classes to model the JSON:



public class Course
{
public string Name { get; set; }
public string Category { get; set; }
public string Headline { get; set; }
public string CreatedBy { get; set; }
public string ExternalImage { get; set; }
public string Duration { get; set; }
public Description Description { get; set; }
}

public class Description
{
[JsonProperty("What You'll Learn")]
public List<string> WhatYoullLearn { get; set; }
public List<string> Requirements { get; set; }
[JsonProperty("Description")]
public string DescriptionHtml { get; set; }
}


Then, I would suggest deserialising into the class, as follows:



var json = File.ReadAllText("/data.json");
var courses = JsonConvert.DeserializeObject<Dictionary<string, Course>>(json);
foreach (var course in courses)
{
var courseName = course.Key;
var courseDetails = course.Value;
// Do something with the data, e.g save it to your database
}





share|improve this answer
























  • Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,Tutorial.ViewModel.js]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

    – Dummies EBooks
    Nov 28 '18 at 13:15











  • It's works , i just removed the [ ] between the start and end .. thanks for helping me and i appreciate that

    – Dummies EBooks
    Nov 28 '18 at 13:18











  • @Dummies EBooks Good. I tested with the JSON you provided in the question and it worked for me so not sure which you needed to remove, but I'm glad it worked anyway.

    – Jamie Twells
    Nov 28 '18 at 13:29
















3














I would suggest first to make some classes to model the JSON:



public class Course
{
public string Name { get; set; }
public string Category { get; set; }
public string Headline { get; set; }
public string CreatedBy { get; set; }
public string ExternalImage { get; set; }
public string Duration { get; set; }
public Description Description { get; set; }
}

public class Description
{
[JsonProperty("What You'll Learn")]
public List<string> WhatYoullLearn { get; set; }
public List<string> Requirements { get; set; }
[JsonProperty("Description")]
public string DescriptionHtml { get; set; }
}


Then, I would suggest deserialising into the class, as follows:



var json = File.ReadAllText("/data.json");
var courses = JsonConvert.DeserializeObject<Dictionary<string, Course>>(json);
foreach (var course in courses)
{
var courseName = course.Key;
var courseDetails = course.Value;
// Do something with the data, e.g save it to your database
}





share|improve this answer
























  • Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,Tutorial.ViewModel.js]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

    – Dummies EBooks
    Nov 28 '18 at 13:15











  • It's works , i just removed the [ ] between the start and end .. thanks for helping me and i appreciate that

    – Dummies EBooks
    Nov 28 '18 at 13:18











  • @Dummies EBooks Good. I tested with the JSON you provided in the question and it worked for me so not sure which you needed to remove, but I'm glad it worked anyway.

    – Jamie Twells
    Nov 28 '18 at 13:29














3












3








3







I would suggest first to make some classes to model the JSON:



public class Course
{
public string Name { get; set; }
public string Category { get; set; }
public string Headline { get; set; }
public string CreatedBy { get; set; }
public string ExternalImage { get; set; }
public string Duration { get; set; }
public Description Description { get; set; }
}

public class Description
{
[JsonProperty("What You'll Learn")]
public List<string> WhatYoullLearn { get; set; }
public List<string> Requirements { get; set; }
[JsonProperty("Description")]
public string DescriptionHtml { get; set; }
}


Then, I would suggest deserialising into the class, as follows:



var json = File.ReadAllText("/data.json");
var courses = JsonConvert.DeserializeObject<Dictionary<string, Course>>(json);
foreach (var course in courses)
{
var courseName = course.Key;
var courseDetails = course.Value;
// Do something with the data, e.g save it to your database
}





share|improve this answer













I would suggest first to make some classes to model the JSON:



public class Course
{
public string Name { get; set; }
public string Category { get; set; }
public string Headline { get; set; }
public string CreatedBy { get; set; }
public string ExternalImage { get; set; }
public string Duration { get; set; }
public Description Description { get; set; }
}

public class Description
{
[JsonProperty("What You'll Learn")]
public List<string> WhatYoullLearn { get; set; }
public List<string> Requirements { get; set; }
[JsonProperty("Description")]
public string DescriptionHtml { get; set; }
}


Then, I would suggest deserialising into the class, as follows:



var json = File.ReadAllText("/data.json");
var courses = JsonConvert.DeserializeObject<Dictionary<string, Course>>(json);
foreach (var course in courses)
{
var courseName = course.Key;
var courseDetails = course.Value;
// Do something with the data, e.g save it to your database
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 28 '18 at 12:58









Jamie TwellsJamie Twells

68721132




68721132













  • Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,Tutorial.ViewModel.js]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

    – Dummies EBooks
    Nov 28 '18 at 13:15











  • It's works , i just removed the [ ] between the start and end .. thanks for helping me and i appreciate that

    – Dummies EBooks
    Nov 28 '18 at 13:18











  • @Dummies EBooks Good. I tested with the JSON you provided in the question and it worked for me so not sure which you needed to remove, but I'm glad it worked anyway.

    – Jamie Twells
    Nov 28 '18 at 13:29



















  • Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,Tutorial.ViewModel.js]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

    – Dummies EBooks
    Nov 28 '18 at 13:15











  • It's works , i just removed the [ ] between the start and end .. thanks for helping me and i appreciate that

    – Dummies EBooks
    Nov 28 '18 at 13:18











  • @Dummies EBooks Good. I tested with the JSON you provided in the question and it worked for me so not sure which you needed to remove, but I'm glad it worked anyway.

    – Jamie Twells
    Nov 28 '18 at 13:29

















Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,Tutorial.ViewModel.js]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

– Dummies EBooks
Nov 28 '18 at 13:15





Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,Tutorial.ViewModel.js]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

– Dummies EBooks
Nov 28 '18 at 13:15













It's works , i just removed the [ ] between the start and end .. thanks for helping me and i appreciate that

– Dummies EBooks
Nov 28 '18 at 13:18





It's works , i just removed the [ ] between the start and end .. thanks for helping me and i appreciate that

– Dummies EBooks
Nov 28 '18 at 13:18













@Dummies EBooks Good. I tested with the JSON you provided in the question and it worked for me so not sure which you needed to remove, but I'm glad it worked anyway.

– Jamie Twells
Nov 28 '18 at 13:29





@Dummies EBooks Good. I tested with the JSON you provided in the question and it worked for me so not sure which you needed to remove, but I'm glad it worked anyway.

– Jamie Twells
Nov 28 '18 at 13:29




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53519148%2fconvert-json-tolist-and-parse-it-to-ef-model%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks