【发布时间】:2018-08-22 22:14:12
【问题描述】:
您好,我尝试在 json 中获取 json 数据。但是我的班级是 Employee 我的服务将 json 创建为 com.myteam.rbffiyatlama2.Employee 这个前缀可以更改,所以我必须编写一个解决方案来获取 json 的确切部分,如下所示,但我的下面的代码不起作用。我会将我的节点名称发送到 Getjsonobject(Employee emp) 或 Getjsonobject(Customer cust) 或 Getjsonobject(Student student) 等方法。
我的 Json:
{
"type": "SUCCESS",
"msg": "Container RBFFiyatlama2_1.0.1 successfully called.",
"result": {"execution-results": {
"results": [
{
"value": 2,
"key": ""
},
{
"value": {"com.myteam.rbffiyatlama2.Employee": {
"salary": 2400,
"age": 35,
"cofactor": 0.2
}},
"key": "t1"
},
{
"value": {"com.myteam.rbffiyatlama2.Employee": {
"salary": 4800,
"age": 35,
"cofactor": 0.2
}},
"key": "t2"
}
],
"facts": [
{
"value": {"org.drools.core.common.DefaultFactHandle": {"external-form": "0:50:1980606587:1980606587:100:DEFAULT:NON_TRAIT:com.myteam.rbffiyatlama2.Employee"}},
"key": "t1"
},
{
"value": {"org.drools.core.common.DefaultFactHandle": {"external-form": "0:51:2052360932:2052360932:99:DEFAULT:NON_TRAIT:com.myteam.rbffiyatlama2.Employee"}},
"key": "t2"
}
]
}}
}
class Program
{
static void Main(string[] args)
{
var employee1 = new Employee() { age = 35, cofactor = 0.2, salary = 2000 };
var employee2 = new Employee() { age = 35, cofactor = 0.2, salary = 4000 };
var list = new List<Employee>();
list.Add(employee1);
list.Add(employee2);
var uri = new Uri("http://localhost:8080/kie-server/services/rest/server/containers/instances/RBFFiyatlama2_1.0.1");
var kieclient = new KieRequestWrapper<Employee>(uri, "kieserver", "@test2018", MethodType.POST, "application/json").Add(list).Run();
Console.Write(kieclient.Content);
var match = Regex.Match(kieclient.Content, @"(?*.Employee{*})");
var result= MyParser.Parse(match, typeof(Employee)); //Desired
Console.Read();
}
}
public class Employee
{
public int age { get; set; }
public double cofactor { get; set; }
public int salary { get; set; }
}
【问题讨论】:
-
简短的回答,你不,你使用像 Json.Net 这样的 json 解析器
-
我认为以下内容也适用于这个问题,因为 JSON 也不是常规语言:stackoverflow.com/a/1732454/5311735
-
既然已经有专用的内置函数,为什么还要重新发明一个正则表达式?
标签: c# .net json regex json.net