1

I want to serialize/deserialize following classes into/from JSON:

public class Employee 
{
    string name;
    Position position;
}

public class Position 
{
    string positionName;
    int salary;
}

The tricky part is that I want to treat Position fields as Employee fields, so JSON would look like this:

{
    "name": "John",
    "positionName": "Manager",
    "salary" : 1000
}

How to achieve this using Json.NET ?

4 Answers 4

2

You have either to deserialize it as anonymous object either (recommended ) implement a custom deserialization as stated here:

Merge two objects during serialization using json.net?

Please let us know if there any more questions.

Here's an example (you can find it in the provided link):

public class FlattenJsonConverter : JsonConverter
{
    public override void WriteJson(JsonWriter writer, object value, 
        JsonSerializer serializer)
    {
        JToken t = JToken.FromObject(value);
        if (t.Type != JTokenType.Object)
        {
            t.WriteTo(writer);
            return;
        }

        JObject o = (JObject)t;
        writer.WriteStartObject();
        WriteJson(writer, o);
        writer.WriteEndObject();
    }

    private void WriteJson(JsonWriter writer, JObject value)
    {
        foreach (var p in value.Properties())
        {
            if (p.Value is JObject)
                WriteJson(writer, (JObject)p.Value);
            else
                p.WriteTo(writer);
        }
    }

    public override object ReadJson(JsonReader reader, Type objectType, 
       object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override bool CanConvert(Type objectType)
    {
        return true; // works for any type
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I would be grateful for an example.
2

My solution is this

static void Main(string[] args)
        {
            Position p = new Position();
            p.positionName = "Manager";
            p.salary = 1000;

            Employee e = new Employee();
            e.name = "John";
            e.position = p;

            ResultJson r = new ResultJson();
            r.name = e.name;
            r.positionName = e.position.positionName;
            r.salary = e.position.salary;

            var result = JsonConvert.SerializeObject(r);

            Console.WriteLine(result);
            Console.ReadLine();
        }

    }

    public class Employee
    {
        public string name { get; set; }
        public Position position { get; set; }
    }

    public class Position
    {
        public string positionName { get; set; }
        public int salary { get; set; }
    }

    public class ResultJson
    {
        public string name { get; set; }
        public string positionName { get; set; }
        public int salary { get; set; }
    }

use seperate model for result

1 Comment

wouldn't recommend it as this solution is very specific and require a model class for every type of result that will be needed
-1

You can use this code with NewtonSoft.Json library

[JsonObject]
public class Employee 
{
    [JsonProperty("name")]
    string name;
    [JsonProperty("positionName")]
    string positionName;
    [JsonProperty("salary")]
    int salary;
}

Use One class instead 2, or realize own parser

1 Comment

this will not give the result the op asked for
-2

try in this way. { "name": "John", "position": [{ "positionName": "Manager", "salary" : 1000 }] }

2 Comments

This won't work. Moreover, I want to both serialize and deserialize.
try removing "[ ]"(square bracket ). for more you can refer msdn.microsoft.com/en-us/library/bb412179(v=vs.110).aspx

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.