如何使用正则表达式从JSON结构中提取子节点?

时间:2018-03-14 11:36:51

标签: c# .net json regex json.net

您好我尝试在json中获取json数据。但是我的班级是员工我的服务创建json为com.myteam.rbffiyatlama2.Employee这个前缀可以更改,所以我必须编写一个解决方案来获取json的确切部分如下所示,但我的下面的代码不起作用。我会将节点名称发送到方法Getjsonobject(Employee emp)Getjsonobject(Customer cust)Getjsonobject(Student student)等。

我的杰森:


{
   "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; }
}

1 个答案:

答案 0 :(得分:3)

您不希望使用XPath来获取所需的数据,您希望将JSON字符串反序列化为对象,然后获取所需的数据。有很多JSON序列化库,最常见的是AFAIK,JSON.NET。您可以在此处查看反序列化的工作原理:https://www.newtonsoft.com/json/help/html/DeserializeObject.htm

示例:

public class Account
{
    public string Email { get; set; }
    public bool Active { get; set; }
    public DateTime CreatedDate { get; set; }
    public IList<string> Roles { get; set; }
}

string json = @"{
  'Email': 'james@example.com',
  'Active': true,
  'CreatedDate': '2013-01-20T00:00:00Z',
  'Roles': [
    'User',
    'Admin'
  ]
}";

Account account = JsonConvert.DeserializeObject<Account>(json);

Console.WriteLine(account.Email);
// james@example.com