1

I try to parse json data to List<Employee> instance. But I don't want to use Json-to-C# tool for creating a shadow from json pattern. I want to take values only. Maybe I can use keys to take values (Employee). I want to fill a List of Employee.

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.0,
                     "age":35,
                     "cofactor":0.2
                  }
               },
               "key":"Employee0"
            },
            {
               "value":{
                  "com.myteam.rbffiyatlama2.Employee":{
                     "salary":4800.0,
                     "age":35,
                     "cofactor":0.2
                  }
               },
               "key":"Employee1"
            }
         ],
         "facts":[
            {
               "value":{
                  "org.drools.core.common.DefaultFactHandle":{
                     "external-form":"0:88:1504512052:1504512052:160:DEFAULT:NON_TRAIT:com.myteam.rbffiyatlama2.Employee"
                  }
               },
               "key":"Employee0"
            },
            {
               "value":{
                  "org.drools.core.common.DefaultFactHandle":{
                     "external-form":"0:89:213603577:213603577:159:DEFAULT:NON_TRAIT:com.myteam.rbffiyatlama2.Employee"
                  }
               },
               "key":"Employee1"
            }
         ]
      }
   }
}

How can I fill Employee without creating any C# class by using above json.

public class Employee
{
    public int age { get; set; }
    public double cofactor { get; set; }
    public int salary { get; set; }
}
Omar Muscatello
  • 1,256
  • 14
  • 25
loki
  • 2,926
  • 8
  • 62
  • 115

1 Answers1

0

You could use dynamics. The following should do what you're after and it's all native:

string jsonString = YourGetJsonStringMethod();
List<Employee> employees = new List<Employee>();
dynamic data = System.Web.Helpers.Json.Decode(jsonString);
dynamic results = data["result"]["execution-results"]["results"];
if (results.Length > 1)
{
    for (var i = 1; i < results.Length; i++)
    {
        var dynamicEmployee = results[i]["value"]["com.myteam.rbffiyatlama2.Employee"];
        dynamicEmployee["salary"] = (int) dynamicEmployee["salary"];
        var encoded = System.Web.Helpers.Json.Encode(dynamicEmployee);
        employees.Add(System.Web.Helpers.Json.Decode<Employee>(encoded));
    }
}

You will obviously need to include System.Web.Helpers in your references, which you can find under Assemblies > Extensions in your Visual Studio Reference Manager.

Bear in mind that this code may raise an exception when you're debugging. If so, refer to this question for the solution.

This code "just works". I will leave it to you to do validation, null-checking and exception catching.

Iain Fraser
  • 6,578
  • 8
  • 43
  • 68
  • To be honest, re-encoding and then decoding each result is a bit wasteful, but I wanted to give you a totally hands-off approach. It would be better to just take the values from the `dynamic` and populate a new `Employee` with it manually. – Iain Fraser Mar 14 '18 at 07:10
  • As others have pointed out, you could also use Json.NET instead of System.Web.Helpers. – Iain Fraser Mar 14 '18 at 08:33