|
@@ -1,27 +1,37 @@
|
|
|
-**["Получение данных из внешних источников.CSV."](https://github.com/kolei/OAP/blob/master/articles/lab_wpf_data_csv.md)**
|
|
|
+**["Получение данных из внешних источников.JSON."](https://github.com/kolei/OAP/blob/master/articles/lab_wpf_data_json.md)**
|
|
|
***
|
|
|
**C#**
|
|
|
***
|
|
|
**Class1**
|
|
|
***
|
|
|
```
|
|
|
+using System;
|
|
|
+using WpfApp1;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
-using System.Security.Cryptography.X509Certificates;
|
|
|
+using System.Runtime.Serialization;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
namespace WpfApp1.model
|
|
|
{
|
|
|
+ [DataContract]
|
|
|
public class cars
|
|
|
{
|
|
|
+ [DataMember]
|
|
|
public string name { get; set; }
|
|
|
+ [DataMember]
|
|
|
public int year { get; set; }
|
|
|
+ [DataMember]
|
|
|
public double price { get; set; }
|
|
|
+ [DataMember]
|
|
|
public string color { get; set; }
|
|
|
+ [DataMember]
|
|
|
public bool defects { get; set; }
|
|
|
- public DateOnly dateOfLastSTO { get; set; }
|
|
|
- public string photo { get; set; }
|
|
|
+ [DataMember]
|
|
|
+ public DateOnly? dateOfLastSTO { get; set; }
|
|
|
+ [DataMember]
|
|
|
+ public string? photo { get; set; }
|
|
|
}
|
|
|
}
|
|
|
```
|
|
@@ -65,7 +75,6 @@ namespace WpfApp1.model
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
```
|
|
|
***
|
|
|
**Class3**
|
|
@@ -90,83 +99,182 @@ namespace WpfApp1.model
|
|
|
public string title { get; set; }
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
```
|
|
|
***
|
|
|
**Class4**
|
|
|
***
|
|
|
```
|
|
|
+using Newtonsoft.Json;
|
|
|
using System;
|
|
|
+using WpfApp1;
|
|
|
using System.Collections.Generic;
|
|
|
-using System.Diagnostics;
|
|
|
-using System.Globalization;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
+using System.Runtime.Serialization;
|
|
|
+using System.Runtime.Serialization.Json;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
-using CsvHelper;
|
|
|
-
|
|
|
|
|
|
namespace WpfApp1.model
|
|
|
{
|
|
|
- class Class4
|
|
|
+ public class JSONDataProvider : LocalDataProvider, IDataProvider
|
|
|
{
|
|
|
- public class CSVDataProvider : LocalDataProvider, IDataProvider
|
|
|
- {
|
|
|
- private IEnumerable<cars> carList;
|
|
|
+ private List<CarWithStringDates> _CarList;
|
|
|
|
|
|
- public CSVDataProvider()
|
|
|
+ public JSONDataProvider()
|
|
|
+ {
|
|
|
+ using (var sr = new StreamReader("./Test.json"))
|
|
|
{
|
|
|
- using (var reader = new StreamReader("./data.csv"))
|
|
|
- {
|
|
|
- using (var csv = new CsvReader(
|
|
|
- reader,
|
|
|
- CultureInfo.InvariantCulture))
|
|
|
- {
|
|
|
- carList = csv.GetRecords<cars>().ToList();
|
|
|
- }
|
|
|
- }
|
|
|
+ _CarList = JsonConvert.DeserializeObject<CarWithStringDates[]>(sr.ReadToEnd()).ToList();
|
|
|
+
|
|
|
}
|
|
|
- public IEnumerable<cars>getCars()
|
|
|
+ }
|
|
|
+
|
|
|
+ public new IEnumerable<cars> getCars()
|
|
|
+ {
|
|
|
+ return _CarList.Select(p => new cars
|
|
|
{
|
|
|
- return carList;
|
|
|
- }
|
|
|
+ name = p.name,
|
|
|
+
|
|
|
+ year = p.year,
|
|
|
+
|
|
|
+ price = p.price,
|
|
|
+
|
|
|
+ color = p.color,
|
|
|
+
|
|
|
+ defects = p.defects,
|
|
|
+
|
|
|
+ photo = p.photo,
|
|
|
+
|
|
|
+ dateOfLastSTO = p.dateOfLastSTO == null ? null : DateOnly.Parse(p.dateOfLastSTO),
|
|
|
+
|
|
|
+ });
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
```
|
|
|
***
|
|
|
-**data.csv**
|
|
|
+**Class5**
|
|
|
***
|
|
|
```
|
|
|
-name,year,price,color,defects,dateOfLastSTO,photo,
|
|
|
-Toyota Trueno AE8,1995,15000,white,false,4/30/2024,,
|
|
|
-Toyota Supra A80,1996,30000,blue,false,5/1/2024,,
|
|
|
-Nissal Skyline R34,1996,25000,red,true,5/2/2024,,
|
|
|
-Nissan Silvia S15,1999,20000,green,false,5/3/2024,,
|
|
|
-Toyota Camry 3.5,2020,20000,black,true,5/4/2024,,
|
|
|
-Audi RS 6,2016,30000,purple,false,5/5/2024,,
|
|
|
-Трактор LOVOL TE354 HT,2024,66666,red,false,5/6/2024,,
|
|
|
-BMW M5 F90,2019,30000,white,true,5/7/2024,,
|
|
|
-BMW E36,2006,35000,green,true,5/8/2024,,
|
|
|
-Daewoo Matiz,2010,20000,blue,false,5/9/2024,,
|
|
|
+using System;
|
|
|
+using WpfApp1;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Runtime.Serialization;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+
|
|
|
+namespace WpfApp1.model
|
|
|
+{
|
|
|
+ [DataContract]
|
|
|
+ public class CarWithStringDates : cars
|
|
|
+ {
|
|
|
+ [DataMember]
|
|
|
+ public string? dateOfLastSTO { get; set; }
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
+```
|
|
|
+***
|
|
|
+**Test.json**
|
|
|
+***
|
|
|
+```
|
|
|
+[
|
|
|
+ {
|
|
|
+ "name": "Toyota Trueno AE86",
|
|
|
+ "year": 1995,
|
|
|
+ "price": 15000,
|
|
|
+ "color": "white",
|
|
|
+ "defects": false,
|
|
|
+ "dateOfLastSTO": "2024-12-05",
|
|
|
+ "photo": null
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "name": "Toyota Supra A80",
|
|
|
+ "year": 1996,
|
|
|
+ "price": 30000,
|
|
|
+ "color": "white",
|
|
|
+ "defects": false,
|
|
|
+ "dateOfLastSTO": "2023-10-02",
|
|
|
+ "photo": null
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "name": "Nissal Skyline R34",
|
|
|
+ "year": 1996,
|
|
|
+ "price": 25000,
|
|
|
+ "color": "gray",
|
|
|
+ "defects": false,
|
|
|
+ "dateOfLastSTO": "2024-11-05",
|
|
|
+ "photo": null
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "name": "Toyota Camry 3.5",
|
|
|
+ "year": 2020,
|
|
|
+ "price": 20000,
|
|
|
+ "color": "black",
|
|
|
+ "defects": false,
|
|
|
+ "dateOfLastSTO": "2022-12-10",
|
|
|
+ "photo": null
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "name": "Audi RS 65",
|
|
|
+ "year": 2016,
|
|
|
+ "price": 30000,
|
|
|
+ "color": "black",
|
|
|
+ "defects": false,
|
|
|
+ "dateOfLastSTO": "2023-12-07",
|
|
|
+ "photo": null
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "name": "Трактор LOVOL TE354 HT",
|
|
|
+ "year": 2024,
|
|
|
+ "price": 66666,
|
|
|
+ "color": "blue",
|
|
|
+ "defects": false,
|
|
|
+ "dateOfLastSTO": "2021-12-10",
|
|
|
+ "photo": null
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "name": "BMW M5 F90",
|
|
|
+ "year": 2019,
|
|
|
+ "price": 30000,
|
|
|
+ "color": "red",
|
|
|
+ "defects": false,
|
|
|
+ "dateOfLastSTO": "2024-04-10",
|
|
|
+ "photo": null
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "name": "BMW E36",
|
|
|
+ "year": 2006,
|
|
|
+ "price": 35000,
|
|
|
+ "color": "black",
|
|
|
+ "defects": false,
|
|
|
+ "dateOfLastSTO": "2019-12-17",
|
|
|
+ "photo": null
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "name": "Daewoo Matiz",
|
|
|
+ "year": 2010,
|
|
|
+ "price": 20000,
|
|
|
+ "color": "blue",
|
|
|
+ "defects": false,
|
|
|
+ "dateOfLastSTO": "2021-12-10",
|
|
|
+ "photo": null
|
|
|
+ }
|
|
|
+]
|
|
|
```
|
|
|
***
|
|
|
**MainWindow.xaml.cs**
|
|
|
***
|
|
|
```
|
|
|
-using CsvHelper;
|
|
|
using System;
|
|
|
using System.ComponentModel;
|
|
|
-using System.Globalization;
|
|
|
-using System.IO;
|
|
|
using System.Windows;
|
|
|
using System.Windows.Controls;
|
|
|
using System.Windows.Input;
|
|
|
using WpfApp1.model;
|
|
|
-using static WpfApp1.model.Class4;
|
|
|
|
|
|
namespace WpfApp1
|
|
|
{
|
|
@@ -219,7 +327,7 @@ namespace WpfApp1
|
|
|
else res = res.OrderByDescending(c => c.year);
|
|
|
|
|
|
return res;
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
set
|
|
|
{
|
|
@@ -233,7 +341,7 @@ namespace WpfApp1
|
|
|
{
|
|
|
InitializeComponent();
|
|
|
DataContext = this;
|
|
|
- Globals.dataProvider = new CSVDataProvider();
|
|
|
+ Globals.dataProvider = new JSONDataProvider();
|
|
|
CarList = Globals.dataProvider.getCars();
|
|
|
CarinfoList = Globals.dataProvider.getName().ToList();
|
|
|
CarinfoList.Insert(0, new Namecar { title = "Все автомобили" });
|
|
@@ -403,9 +511,7 @@ namespace WpfApp1
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
+}
|
|
|
```
|
|
|
***
|
|
|
**MainWindow.xaml**
|
|
@@ -432,7 +538,7 @@ namespace WpfApp1
|
|
|
|
|
|
<!-- лого -->
|
|
|
<Image
|
|
|
- Source="./img/da.jpg"
|
|
|
+ Source="./img/net.jpg"
|
|
|
Grid.RowSpan="2" HorizontalAlignment="Right"/>
|
|
|
<DataGrid
|
|
|
Grid.Row="1"
|
|
@@ -445,17 +551,20 @@ namespace WpfApp1
|
|
|
Header="Название"
|
|
|
Binding="{Binding name}"/>
|
|
|
<DataGridTextColumn
|
|
|
+ Header="Год"
|
|
|
+ Binding="{Binding year}"/>
|
|
|
+ <DataGridTextColumn
|
|
|
Header="Цена"
|
|
|
Binding="{Binding price}"/>
|
|
|
<DataGridTextColumn
|
|
|
- Header="Год"
|
|
|
- Binding="{Binding year}"/>
|
|
|
+ Header="Цвет"
|
|
|
+ Binding="{Binding color}"/>
|
|
|
<DataGridTextColumn
|
|
|
- Header="Дефекты"
|
|
|
- Binding="{Binding defects}"/>
|
|
|
+ Header="Повреждения"
|
|
|
+ Binding="{Binding defects}"/>
|
|
|
<DataGridTextColumn
|
|
|
- Header="Дата последнего СТО"
|
|
|
- Binding="{Binding dateOfLastSTO}"/>
|
|
|
+ Header="Дата СТО"
|
|
|
+ Binding="{Binding dateOfLastSTO}"/>
|
|
|
</DataGrid.Columns>
|
|
|
</DataGrid>
|
|
|
<StackPanel
|
|
@@ -468,7 +577,7 @@ namespace WpfApp1
|
|
|
Click="ExitButton_Click"
|
|
|
Height="50"/>
|
|
|
</StackPanel>
|
|
|
-
|
|
|
+
|
|
|
|
|
|
<WrapPanel
|
|
|
Orientation="Horizontal"
|
|
@@ -482,7 +591,7 @@ namespace WpfApp1
|
|
|
VerticalAlignment="Center"
|
|
|
x:Name="SearchFilterTextBox"
|
|
|
KeyUp="SearchFilter_KeyUp"/>
|
|
|
-
|
|
|
+
|
|
|
<Label
|
|
|
Content="Название:"
|
|
|
VerticalAlignment="Center"/>
|
|
@@ -504,7 +613,7 @@ namespace WpfApp1
|
|
|
<Label
|
|
|
Content="Цена:"
|
|
|
VerticalAlignment="Center"/>
|
|
|
-
|
|
|
+
|
|
|
<ComboBox
|
|
|
Name="carpriceFilterComboBox"
|
|
|
SelectionChanged="carpriceFilterComboBox_SelectionChanged"
|
|
@@ -543,7 +652,5 @@ ItemsSource="{Binding CarpriceList}">
|
|
|
```
|
|
|
***
|
|
|
**Вот что получилось**
|
|
|
-
|
|
|
-![](./img/tea2.png)
|
|
|
***
|
|
|
-![](./img/tea.png)
|
|
|
+![](./img/qqq.png)
|