Newtonsoft.Json の使い方
基本ここの確認 docs.microsoft.com
- [ファイル]、[新しいプロジェクト] の順に使用し、[Windows ユニバーサル]、[空白のアプリ (ユニバーサル Windows)] の順に選択して、Visual Studio でプロジェクトを作成します。 プロンプトが表示されたら、ターゲット バージョンと最小バージョンの既定値を受け入れます。
- Newtonsoft.Json のインストール
- [ツール]、[NuGet パッケージ マネージャー]、[パッケージ マネージャー コンソール] メニュー コマンドの順に選択します。
- MainPage.xamlを開いて、既存の Grid 要素を次の XAML に置き換えます。
- MainPage.xaml.cs ファイル (ソリューション エクスプローラーの MainPage.xamlノードの下にあります) を開いて、MainPageコンストラクター内に次のコードを挿入します。
- プロジェクトに Newtonsoft.Json パッケージを追加した後でも、JsonConvert の下に赤い波線が表示されます。これは、コード ファイルの上部に using ステートメント (using Newtonsoft.json;) が必要であるためです。
- アプリをビルドして実行します。F5 キーを押すか、[デバッグ]、[デバッグの開始] を選択してください。ボタンを選択し、TextBlock の内容が JSON テキストに置換されたことを確認します。
(参考コード)
using Newtonsoft.Json; using System; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace App_201810301 { public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } public class Account { public string Name { get; set; } public string Email { get; set; } public DateTime DOB { get; set; } } private void Button_Click(object sender, RoutedEventArgs e) { Account account = new Account { Name = "John Doe", Email = "john@microsoft.com", DOB = new DateTime(1980, 2, 20, 0, 0, 0, DateTimeKind.Utc), }; string json = JsonConvert.SerializeObject(account, Formatting.Indented); TextBlock.Text = json; } } }
<Page x:Class="App_201810301.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App_201810301" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <StackPanel VerticalAlignment="Center"> <Button Click="Button_Click" Content="Click Me" Margin="10"/> <TextBlock Name="TextBlock" Text="TextBlock" Margin="10"/> </StackPanel> </Grid> </Grid> </Page>