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.

61 lines
2.2 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);
//rawBarCodeInfoList.Add(new RawBarCodeInfo());
return rawBarCodeInfoList.Cast<object>().ToList();
}
else if (output.Template == "product")
{
var productBarCodeInfoList = JsonSerializer.Deserialize<List<ProductBarCodeInfo>>(output.PrintContent);
// productBarCodeInfoList.Add(new ProductBarCodeInfo());
return productBarCodeInfoList.Cast<object>().ToList();
}
else if(output.Template == "bind")
{
var bindBarCodeInfoList = JsonSerializer.Deserialize<List<BindBarCodeInfo>>(output.PrintContent);
//bindBarCodeInfoList.Add(new BindBarCodeInfo());
return bindBarCodeInfoList.Cast<object>().ToList();
}
else if(output.Template == "small")
{
var smallCodeInfoList = JsonSerializer.Deserialize<List<SmallCodeInfo>>(output.PrintContent);
// smallCodeInfoList.Add(new SmallCodeInfo());
return smallCodeInfoList.Cast<object>().ToList();
}
else
{
return null;
}
}
catch (JsonException ex)
{
Console.WriteLine($"Failed to parse JSON: {ex.Message}");
return null;
}
}
}
}