Windows::Data::Json::JsonObject
Visual Studio 2017 のUWP VC++ でJSONデータの生成方法です。
JsonObjectは、JSONデータの生成ができます。
xaml側でJSON生成するボタンと出来上がったJSONを表示するためのTextBoxを用意します。
<Button Name="Button1" Content="JSON TEST" Width="400" Height="40" HorizontalAlignment="Left" Margin="51,53,0,0" VerticalAlignment="Top" Click="Button1_Click" /> <TextBox Name="Edit1" Width="400" Height="300" HorizontalAlignment="Left" Margin="51,120,0,0" VerticalAlignment="Top" Background="Transparent" AcceptsReturn="True" TextWrapping="Wrap" ></TextBox>
Button1_Clickイベントを作成しているので、そのイベント内でJsonObjectを使いデータを生成します。
void cpp_uwp::json_test::Button1_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) { std::array<Platform::String^, 5> ultra_list{"ルーブ","ジード","オーブ","ゼロ","メビウス"}; using namespace Windows::Data::Json; JsonObject^ JsonRoot = ref new JsonObject(); JsonRoot->Insert("name", JsonValue::CreateStringValue("毛利")); JsonArray^ JsonArray1 = ref new JsonArray(); for (auto& s: ultra_list) { JsonArray1->Append(JsonValue::CreateStringValue(s)); } JsonRoot->Insert("ultra", JsonArray1); JsonRoot->Insert("age", JsonValue::CreateNumberValue(17)); JsonObject^ JsonObject2 = ref new JsonObject(); JsonObject2->Insert("read", JsonValue::CreateBooleanValue(true)); JsonObject2->Insert("address_type", JsonValue::CreateStringValue("JP")); JsonRoot->Insert("priority", JsonObject2); Edit1->Text = JsonRoot->Stringify(); }
出力されたJSONデータ
{ "name": "毛利", "ultra": [ "ルーブ", "ジード", "オーブ", "ゼロ", "メビウス" ], "age": 17, "priority": { "read": true, "address_type": "JP" } }