系统JSON测试通过

This commit is contained in:
PeterZhong 2025-05-13 23:59:58 +08:00
parent ac13f2708c
commit f341ac9aac
2 changed files with 70 additions and 11 deletions

View File

@ -29,6 +29,7 @@
</WrapPanel>
</TextBlock.ToolTip>
</TextBlock>
<Button Content="Test Server" Name="TestButton" Click="TestButton_OnClick" ></Button>
</DockPanel>
</Grid>
</UserControl>

View File

@ -1,17 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace LinkToolAddin.ui.dockpane
@ -25,5 +16,72 @@ namespace LinkToolAddin.ui.dockpane
{
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; }
}
}