1

I am using Newtonsoft json serializer to convert json string into objects. I have json structure as below -

{
    "EmployeeRecords": {
        "12": {
            "title": "Mr",
            "Name": "John"
        },
        "35":{
            "title": "Mr",
            "Name": "Json"
        }
    }
}

I want this Json to be serilized into below class -

public class Employee
{
    public string EmployeeNumber { get; set; }
    public string title { get; set; }
    public string Name { get; set; }
}

public class EmployeeRecords
{
    public List<Employee> Employees { get; set; }
}

Here Employeenumber is 12 and 35.

Please guide me how can I write custom serilizer which will read the Employee number from parent node and include it in the child node's EmployeeNumber property.

4
  • try to use this solution: stackoverflow.com/questions/7895105/… Commented Jan 16, 2015 at 7:22
  • Are you required by business rules to create that JSON? It's valid but it's very non-standard. Commented Jan 16, 2015 at 7:23
  • Can you modify the JSON? Don't reinvent the wheel! Serialize JSON in a matching class, then convert this class in the one you want. Commented Jan 16, 2015 at 7:25
  • Cannt modify JSON its provided by Business using some CQ5 tool Commented Jan 16, 2015 at 7:36

2 Answers 2

1

You can deserialize into a dictionary, then assign the EmployeeNumbers in a loop.

public class DataModel
{
    public Dictionary<string, Employee> EmployeeRecords { get; set; }
}

Assing the numbers after deserialization:

var records = JsonConvert.DeserializeObject<DataModel>(json);

foreach (var item in records.EmployeeRecords)
{
    item.Value.EmployeeNumber = item.Key;
}
Sign up to request clarification or add additional context in comments.

7 Comments

How to deserialize to dictionary first which will populate Employee object?
@CHash_Mike Change the property name in EmployeeRecords class. I have updated the answer.
Hi Ufuk, I tried this solution but it fails to serialize the json to dictionary. Am I missing something? I am wondering how would serializer know the json object has to be serialized to dictionary?
@CHash_Mike Objects are simply dictionaries in JavaScript. I have changed class name because you can't have property names with the same name. Can you try it again?
Thanks @Ufuk.. The solution is correct but looks like some issue with my class structure generated from Json, the collection is coming as null.
|
0

You can easily do this with LINQ to JSON (JObject and friends). Here's a short but complete example:

using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

public class Employee
{
    public string EmployeeNumber { get; set; }
    public string title { get; set; }
    public string Name { get; set; }

    public JProperty ToJProperty()
    {
        return new JProperty(EmployeeNumber,
            new JObject {
                { "title", title },
                { "Name", Name }
            });
    }
}

public class EmployeeRecords
{
    public List<Employee> Employees { get; set; }

    public JObject ToJObject()
    {
        var obj = new JObject();
        foreach (var employee in Employees)
        {
            obj.Add(employee.ToJProperty());
        }
        return new JObject {
            new JProperty("EmployeeRecords", obj)
        };
    }
}

class Test
{
    static void Main()
    {
        var records = new EmployeeRecords {
            Employees = new List<Employee> {
                new Employee {
                    EmployeeNumber = "12",
                    title = "Mr",
                    Name = "John"
                },
                new Employee {
                    EmployeeNumber = "35",
                    title = "Mr",
                    Name = "Json"
                },
            }
        };
        Console.WriteLine(records.ToJObject());
    }    
}

It may not be the simplest code (Ufuk's approach is great if you're happy to change your structuring), but it shows how customizable everything is.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.