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; }
}
3
  • 3
    Short answer, is you dont, you use a json parser like Json.Net Commented Mar 14, 2018 at 11:38
  • I think the following applies to this question as well, since JSON is also not regular language: stackoverflow.com/a/1732454/5311735 Commented Mar 14, 2018 at 11:46
  • why would you reinvent a regex since there is already dedicated builtin ? Commented Mar 14, 2018 at 12:02

1 Answer 1

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': '[email protected]',
  'Active': true,
  'CreatedDate': '2013-01-20T00:00:00Z',
  'Roles': [
    'User',
    'Admin'
  ]
}";

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

Console.WriteLine(account.Email);
// [email protected]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.