【问题标题】:How can I use regex to extract child node from JSON structure?如何使用正则表达式从 JSON 结构中提取子节点?
【发布时间】: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


【解决方案1】:

您不想使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 2021-10-29
    相关资源
    最近更新 更多