0

Hi I try to get json data inside of the json. But my class is Employee my service creates json as com.myteam.rbffiyatlama2.Employee this prefix can be changeable so I have to write a solution to get an exact part of the json Like below but my below code is not working. I will send my node name to a method Getjsonobject(Employee emp) or Getjsonobject(Customer cust) or Getjsonobject(Student student) etc.

My 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; }
}
xsami
  • 1,312
  • 16
  • 31
loki
  • 2,926
  • 8
  • 62
  • 115

1 Answers1

3

You don't want to use XPath to get the data you need, you want to deserialize the the JSON string into an object and then get the data you need. There are many JSON serialization libraries out there, the most common one, AFAIK, is JSON.NET. You can look at how deserialization works here: https://www.newtonsoft.com/json/help/html/DeserializeObject.htm

Example:

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
Andre Pena
  • 56,650
  • 48
  • 196
  • 243