0

I have to: DeserializeObject - do some changes - SerializeObject When my Json have miltiple types e.g.

{
  "type": "test",
  "currentStatus": "Active",
  "id": "987",
  "items": [
    {
      "type": "test1",
      "id": "123",
      "name": "Segment Members",
      "memberCount": "0",
      "outputTerminals": [
        {
          "type": "test2",
          "id": "123",
          "connectedId": "123",
          "terminalType": "out"
        }
      ],
      "position": {
        "type": "Position",
        "x": "46",
        "y": "14"
      },
      "isFinished": "true",
      "isRecurring": "false",
      "segmentId": "123"
    },
    {
      "type": "test5",
      "id": "1390",
      "name": "Yay! Clickers",
      "memberCount": "2",
      "position": {
        "type": "Position",
        "x": "330",
        "y": "375"
      },
      "waitFor": "2592000"
    },
    {
      "type": "test3",
      "id": "1391",
      "name": "test",
      "memberCount": "73",
      "outputTerminals": [
        {
          "type": "test4",
          "id": "123",
          "connectedId": "123",
          "connectedType": "CampaignWaitAction",
          "terminalType": "yes"
        },
        {
          "type": "test4",
          "id": "123",
          "connectedId": "123",
          "connectedType": "CampaignWaitAction",
          "terminalType": "no"
        }
      ],
      "position": {
        "type": "Position",
        "x": "123",
        "y": "123"
      },
      "testId": "123"
    }
  ]
}

What data type i should use for this operation? dynamic, object, Jobject...? or maybe something else?

1
  • 3
    Try it out and be surprised, because you learned something today! Commented Oct 25, 2016 at 11:14

3 Answers 3

3

I would just create an object and deseriablize it to that type. A lot easier than doing it dynamically. (assuming it will stay in that structure always)

WHAT TO DO

Copy your Json:

 `Edit` -> `Paste Special` -> `Paste JSON As CLASSES` 

And there you go! You have the Type you want to deserialize to.

var deserializedJson = JsonConvert.DeserializeObject<YourNewObject>(jsonString);

Note: if the pasting of the json class doesn't work, make sure your json is valid: ClickHereToValidateJson

Sign up to request clarification or add additional context in comments.

Comments

1
using Newtonsoft.Json;          
dynamic collection = new
{
  MultipleType = //call function which returns json here
};
//serialize
var jsonSerializedFeed = JsonConvert.SerializeObject(MultipleType);
//deserialize
var jsonSerializedFeed = JsonConvert.DeserializeObject(MultipleType);

Hope it helps

Comments

1

You can create classes and serialize/deserialize json:

 public class OutputTerminal
    {
        public string type { get; set; }
        public string id { get; set; }
        public string connectedId { get; set; }
        public string terminalType { get; set; }
        public string connectedType { get; set; }
    }

    public class Position
    {
        public string type { get; set; }
        public string x { get; set; }
        public string y { get; set; }
    }

    public class Item
    {
        public string type { get; set; }
        public string id { get; set; }
        public string name { get; set; }
        public string memberCount { get; set; }
        public IList<OutputTerminal> outputTerminals { get; set; }
        public Position position { get; set; }
        public string isFinished { get; set; }
        public string isRecurring { get; set; }
        public string segmentId { get; set; }
        public string waitFor { get; set; }
        public string testId { get; set; }
    }

    public class Root
    {
        public string type { get; set; }
        public string currentStatus { get; set; }
        public string id { get; set; }
        public IList<Item> items { get; set; }
    }

And then:

string json = JsonConvert.SerializeObject(rootObject);
Root root = JsonConvert.DeserializeObject<Root>(json);

Also, you can use dynamic type without classes:

string json = JsonConvert.SerializeObject(dynamicObject);
dynamic obj = JsonConvert.DeserializeObject(json);

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.