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 ParseJson(string jsonString) { try { // 解析外层的 JSON 字符串 var output = JsonSerializer.Deserialize(jsonString); // 根据 template 字段选择不同的解析方式 if (output.Template == "raw") { var rawBarCodeInfoList = JsonSerializer.Deserialize>(output.PrintContent); return rawBarCodeInfoList.Cast().ToList(); } else if (output.Template == "product") { var productBarCodeInfoList = JsonSerializer.Deserialize>(output.PrintContent); return productBarCodeInfoList.Cast().ToList(); } else if(output.Template == "bind") { var bindBarCodeInfoList = JsonSerializer.Deserialize>(output.PrintContent); return bindBarCodeInfoList.Cast().ToList(); } else if(output.Template == "small") { var smallCodeInfoList = JsonSerializer.Deserialize>(output.PrintContent); return smallCodeInfoList.Cast().ToList(); } else { return null; } } catch (JsonException ex) { Console.WriteLine($"Failed to parse JSON: {ex.Message}"); return null; } } } }