87 lines
2.4 KiB
C#
87 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text.Json;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
|
|
|
|
namespace LinkToolAddin.ui.dockpane
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for DialogDockpaneView.xaml
|
|
/// </summary>
|
|
public partial class DialogDockpaneView : UserControl
|
|
{
|
|
public DialogDockpaneView()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void TestButton_OnClick(object sender, RoutedEventArgs e)
|
|
{
|
|
string originalJson = @"{
|
|
""name"": ""Alice"",
|
|
""age"": 30,
|
|
""isStudent"": false,
|
|
""hobbies"": [
|
|
""reading"",
|
|
""swimming"",
|
|
""hiking""
|
|
],
|
|
""address"": {
|
|
""street"": ""123 Main St"",
|
|
""city"": ""Anytown"",
|
|
""postalCode"": ""12345""
|
|
}
|
|
}";
|
|
|
|
try
|
|
{
|
|
// 反序列化 JSON 到对象
|
|
Person? person = JsonSerializer.Deserialize<Person>(originalJson);
|
|
|
|
if (person != null)
|
|
{
|
|
// 序列化对象回 JSON 字符串
|
|
string serializedJson = JsonSerializer.Serialize(person, new JsonSerializerOptions
|
|
{
|
|
WriteIndented = true // 格式化输出
|
|
});
|
|
|
|
Console.WriteLine("序列化后的 JSON:");
|
|
Console.WriteLine(serializedJson);
|
|
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(serializedJson);
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("反序列化失败,对象为 null。");
|
|
}
|
|
}
|
|
catch (JsonException ex)
|
|
{
|
|
Console.WriteLine($"JSON 处理错误: {ex.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"发生错误: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
|
|
// 定义数据模型类
|
|
public class Person
|
|
{
|
|
public string? Name { get; set; }
|
|
public int Age { get; set; }
|
|
public bool IsStudent { get; set; }
|
|
public List<string>? Hobbies { get; set; }
|
|
public Address? Address { get; set; }
|
|
}
|
|
|
|
public class Address
|
|
{
|
|
public string? Street { get; set; }
|
|
public string? City { get; set; }
|
|
public string? PostalCode { get; set; }
|
|
}
|
|
} |