Click here to Skip to main content
15,885,365 members
Home / Discussions / C#
   

C#

 
AnswerRe: MessageBoxManager .dll using Pin
Pete O'Hanlon27-Dec-17 22:02
mvePete O'Hanlon27-Dec-17 22:02 
AnswerRe: MessageBoxManager .dll using Pin
ormonds28-Dec-17 9:56
ormonds28-Dec-17 9:56 
GeneralRe: MessageBoxManager .dll using Pin
BillWoodruff29-Dec-17 14:51
professionalBillWoodruff29-Dec-17 14:51 
AnswerRe: MessageBoxManager .dll using Pin
BillWoodruff29-Dec-17 14:56
professionalBillWoodruff29-Dec-17 14:56 
QuestionIssue while parsing HTML string in C# Pin
Mateen1127-Dec-17 18:59
Mateen1127-Dec-17 18:59 
AnswerRe: Issue while parsing HTML string in C# Pin
OriginalGriff27-Dec-17 20:05
mveOriginalGriff27-Dec-17 20:05 
QuestionExtracting a node from Json File in C# Pin
Sandeep Vemulapalli27-Dec-17 11:10
Sandeep Vemulapalli27-Dec-17 11:10 
AnswerRe: Extracting a node from Json File in C# Pin
Nathan Minier28-Dec-17 1:52
professionalNathan Minier28-Dec-17 1:52 
You will find that life will be much, much easier if you write container classes for these JSON objects and deserialize directly into instances of those objects. It'll also help you handle these items as strongly-typed objects in your code. I can tell you've done a bit of XML handling in the past, and it's just a different way of handling it.
C#
public abstract class Product
{
   public decimal Price { get; set; }
}

public class Book : Product
{
   public string Category { get; set; } // This could be a relationship or enum
   public string Title { get; set; }
   public string Author { get; set; }
}

public class Bicycle : Product
{
   public string Type { get; set; } //this would be a good spot for an entity relationship
   public string Color { get; set; } // This could be a relationship or enum

}

public class Store
{
   public string Name { get; set; } 
   public string Address { get; set; }
   public Dictionary<string,List<Product>> Goods { get; set; }

   public Store()
   {
      Goods = new Dictionary<string,List<Product>>();
   }
}

There are a couple of ways to handle the "goods" collection, but this is the easiest and should work with JSON.Net out of the box. How you obtain your object can vary a little, but parsing it directly from the original stream is preferable just for efficiency:
C#
public Store GetStoreInventory(string fileName)
{
   using(var jsonFile = new FileStream(fileName))
   {
      using(var reader = new StreamReader(jsonFile))
      {
         return JsonConvert.DeserializeObject<Store>(reader.ReadToEnd());
      }
   }
}

From here you can handle the data just like you would any normal .NET CLR type, i.e.
C#
var store = GetStoreInventory(FILEPATH);
var firstBook = store.Goods["book"].FirstOrDefault();

The only "gotcha" is that the JSON.NET deserialize does not play nicely with interfaces, so use abstract classes instead (hence Product rather than IProduct).
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli

QuestionC # + Regex help (for a newbie) Pin
Damian Bz27-Dec-17 4:16
Damian Bz27-Dec-17 4:16 
AnswerRe: C # + Regex help (for a newbie) Pin
OriginalGriff27-Dec-17 5:00
mveOriginalGriff27-Dec-17 5:00 
GeneralRe: C # + Regex help (for a newbie) Pin
Nathan Minier27-Dec-17 6:10
professionalNathan Minier27-Dec-17 6:10 
GeneralRe: C # + Regex help (for a newbie) Pin
OriginalGriff27-Dec-17 6:23
mveOriginalGriff27-Dec-17 6:23 
GeneralRe: C # + Regex help (for a newbie) Pin
Nathan Minier27-Dec-17 6:34
professionalNathan Minier27-Dec-17 6:34 
GeneralRe: C # + Regex help (for a newbie) Pin
OriginalGriff27-Dec-17 6:40
mveOriginalGriff27-Dec-17 6:40 
GeneralRe: C # + Regex help (for a newbie) Pin
Nathan Minier27-Dec-17 6:56
professionalNathan Minier27-Dec-17 6:56 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz27-Dec-17 6:24
Damian Bz27-Dec-17 6:24 
GeneralRe: C # + Regex help (for a newbie) Pin
OriginalGriff27-Dec-17 6:28
mveOriginalGriff27-Dec-17 6:28 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz27-Dec-17 6:18
Damian Bz27-Dec-17 6:18 
GeneralRe: C # + Regex help (for a newbie) Pin
OriginalGriff27-Dec-17 6:26
mveOriginalGriff27-Dec-17 6:26 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz27-Dec-17 6:29
Damian Bz27-Dec-17 6:29 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz27-Dec-17 7:37
Damian Bz27-Dec-17 7:37 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz27-Dec-17 9:15
Damian Bz27-Dec-17 9:15 
GeneralRe: C # + Regex help (for a newbie) Pin
Richard MacCutchan27-Dec-17 21:56
mveRichard MacCutchan27-Dec-17 21:56 
GeneralRe: C # + Regex help (for a newbie) Pin
OriginalGriff28-Dec-17 1:51
mveOriginalGriff28-Dec-17 1:51 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz28-Dec-17 6:59
Damian Bz28-Dec-17 6:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.