0

I've been trying to load & deserialize a JSON file into a datagrid for several days now (using JSON.net), and the grid keeps turning up blank. I'm sure it's obvious to most and I'm probably missing some major items... but I'm learning and just can't seem to figure this out. Would appreciate some guidance!

Here's my XAML:

        <DataGrid x:Name="dataGrid" 
              HorizontalAlignment="Stretch" 
              VerticalAlignment="Stretch" 
              AutoGenerateColumns="False"
              ItemsSource="{Binding items}" Margin="0,36,0,0">
        <DataGrid.Columns>
            <DataGridTextColumn Header="projectNumber" Binding="{Binding Path=projectNumber}"/>
            <DataGridTextColumn Header="projectName" Binding="{Binding Path=projectName}"/>
            <DataGridTextColumn Header="Directory1" Binding="{Binding Path=Directory1}"/>
            <DataGridTextColumn Header="Directory2" Binding="{Binding Path=Directory2}"/>
            <DataGridTextColumn Header="Directory3" Binding="{Binding Path=Directory3}"/>
        </DataGrid.Columns>
    </DataGrid>

Here is my backend:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        // deserialize JSON directly from a file
        using (StreamReader file = File.OpenText(@"C:\Projects\JSONimport\projectList.json"))
        {
            JsonSerializer serializer = new JsonSerializer();
            ProjectThread projectThread2 = (ProjectThread)serializer.Deserialize(file, typeof(ProjectThread));
        }
    }
    public void LoadJson()
    {
        using (StreamReader r = new StreamReader(@"C:\Projects\JSONimport\projectList.json"))
        {
            string json = r.ReadToEnd();
            ObservableCollection<ProjectThread> items = JsonConvert.DeserializeObject<ObservableCollection<ProjectThread>>(json);
        }
    } 

}
public class ProjectThread
{
    public String projectNumber { get; set; }
    public String projectName { get; set; }
    public String Directory1 { get; set; }
    public String Directory2 { get; set; }
    public String Directory3 { get; set; }
}

And here's my tester JSON file format:

  {
     "projectNumber": "100",
     "projectName": "sample",
     "Directory1": "x",
     "Directory2": "x",
     "Directory3": "x"
  },
  {
     "projectNumber": "101",
     "projectName": "sample 2",
     "Directory1": "x",
     "Directory2": "x",
     "Directory3": "x"
  },
ctalley5
  • 77
  • 3
  • 13

2 Answers2

0

I changed your code in following way and it worked for me. I think you are binding private field to the datagrid. And you are initializing new object of Items inside the LoadJson function.

ViewModel

 private ObservableCollection<ProjectThread> _items;

        public ObservableCollection<ProjectThread> Items
        {
            get { return _items; }
            set
            {
                _items = value;
                this.NotifyPropertyChanged("Items");
            }
        }

        public ViewModel()
        {
            Items = new ObservableCollection<ProjectThread>();
            this.LoadJson();
        }

        public void LoadJson()
        {
            using (StreamReader r = new StreamReader(@"d:\file.txt"))
            {
                string json = r.ReadToEnd();
                Items = JsonConvert.DeserializeObject<ObservableCollection<ProjectThread>>(json);
            }
        }

Xaml

 <DataGrid x:Name="dataGrid" 
              HorizontalAlignment="Stretch" 
              VerticalAlignment="Stretch" 
              AutoGenerateColumns="False"
              ItemsSource="{Binding Items}" Margin="0,36,0,0">
            <DataGrid.Columns>
                <DataGridTextColumn Header="projectNumber" Binding="{Binding Path=projectNumber}"/>
                <DataGridTextColumn Header="projectName" Binding="{Binding Path=projectName}"/>
                <DataGridTextColumn Header="Directory1" Binding="{Binding Path=Directory1}"/>
                <DataGridTextColumn Header="Directory2" Binding="{Binding Path=Directory2}"/>
                <DataGridTextColumn Header="Directory3" Binding="{Binding Path=Directory3}"/>
            </DataGrid.Columns>
        </DataGrid>

Json

[{
     "projectNumber": "100",
     "projectName": "sample",
     "Directory1": "x",
     "Directory2": "x",
     "Directory3": "x"
  },
  {
     "projectNumber": "101",
     "projectName": "sample 2",
     "Directory1": "x",
     "Directory2": "x",
     "Directory3": "x"
  }]
Justin CI
  • 2,693
  • 1
  • 16
  • 34
  • 1
    Don't forget to set the MainWindow's DataContext to an instance of the ViewModel class. – Clemens Jun 28 '16 at 07:59
  • Guys both of your input worked and helped tremendously. Justin it was also my introduction into MVVM so thanks for specifying your code as in the ViewModel class. Always easier to learn when it applies to something you're working on!!! – ctalley5 Jun 29 '16 at 03:33
0

Guys both of your input worked and helped tremendously. Justin it was also my introduction into MVVM so thanks for specifying your code as in the ViewModel class. Always easier to learn when it applies to something you're working on!!!

ctalley5
  • 77
  • 3
  • 13