You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.0 KiB
C#

using PrintBarCode.DTO;
using PrintBarCode.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace PrintBarCode.Helper
{
public class JsonParser
{
public static List<object> ParseJson(string jsonString)
{
try
{
// 解析外层的 JSON 字符串
var output = JsonSerializer.Deserialize<Output>(jsonString);
// 根据 template 字段选择不同的解析方式
if (output.Template == "raw")
{
var rawBarCodeInfoList = JsonSerializer.Deserialize<List<RawBarCodeInfo>>(output.PrintContent);
return rawBarCodeInfoList.Cast<object>().ToList();
}
else if (output.Template == "product")
{
var productBarCodeInfoList = JsonSerializer.Deserialize<List<ProductBarCodeInfo>>(output.PrintContent);
return productBarCodeInfoList.Cast<object>().ToList();
}
else if(output.Template == "bind")
{
var bindBarCodeInfoList = JsonSerializer.Deserialize<List<BindBarCodeInfo>>(output.PrintContent);
return bindBarCodeInfoList.Cast<object>().ToList();
}
else if(output.Template == "small")
{
var smallCodeInfoList = JsonSerializer.Deserialize<List<SmallCodeInfo>>(output.PrintContent);
return smallCodeInfoList.Cast<object>().ToList();
}
else
{
return null;
}
}
catch (JsonException ex)
{
Console.WriteLine($"Failed to parse JSON: {ex.Message}");
return null;
}
}
}
}