我假设我在这里犯了一个愚蠢的错误,但想知道是否有人可以提供帮助?
我正在尝试以下库:
Install-Package RulesEngine -Version 3.2.0
我的rules.json如下:
[
{
"WorkflowName": "DeliverToMatchingSuburb",
"Rules": [
{
"RuleName": "MatchingSuburb",
"Expression": "customer.PostCode == location.PostCode"
}
]
}
]
我的代码:
string[] readText = File.ReadAllLines("rules.json");
Customer c = new Customer();
Pharmacy p = new Pharmacy();
var pharmacies = p.GetLocations();
var customers = c.GenerateCustomers();
var re = new RulesEngine.RulesEngine(readText, null);
我尝试将“readText”传递到规则引擎中,但出现以下异常?
Newtonsoft.Json.JsonSerializationException: 'Unexpected end when reading JSON. Path '', line 1, position 1.'
异常发生在这一行:var re = new RulesEngine.RulesEngine(readText, null);
- 我已使用在线 JSON 检查 JSON 是否有效 验证器。
- 我还尝试在顶部添加花括号 底部。
- 我什至尝试过从以下位置复制/粘贴示例规则 JSON 文档在这里 https://github.com/microsoft/RulesEngine#how-to-use-it (会心 它不会工作,但至少应该通过创建的步骤 RulesEngine),但也失败并出现相同的错误。
最佳答案
对 RulesEngine(string[], ILogger, ReSettings)
构造函数的期望是字符串数组的每个元素都是完整的 JSON 对象。在您的例子中,您只为每个数组元素提供了一行。
鉴于您的文本文件已包含规则集合,您应该自行反序列化它,并将反序列化的集合传递到接受 WorkflowRules[]
的构造函数中:
string json = File.ReadAllText("rules.json");
var rules = JsonConvert.DeserializeObject<WorkflowRules[]>(json);
var engine = new RulesEngine.RulesEngine(rules);
关于c# - 尝试简单的 microsoft/RulesEngine 概念证明但得到 JsonSerializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68053336/