C# 如果json对象是动态的,则将该对象转换为模型

C# 如果json对象是动态的,则将该对象转换为模型,c#,json,json.net,C#,Json,Json.net,如果json对象是number,并且我想访问该对象的字段,那么我需要将该对象转换为model的解决方案 {"threaded_extended": { "857334186": [ { "id": 871699994, "sender_id": 1621429355, "replied_to_id": 871550113, "created_at": "2017/04/07 09:45:18 +0000", "network_id": 12467,

如果json对象是number,并且我想访问该对象的字段,那么我需要将该对象转换为model的解决方案

{"threaded_extended": {
"857334186": [
  {
    "id": 871699994,
    "sender_id": 1621429355,
    "replied_to_id": 871550113,
    "created_at": "2017/04/07 09:45:18 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/871699994",
    "web_url": "https://www.yammer.com/kcc.com/messages/871699994",
    "group_id": 10601015,
    "body": {
      "parsed": "Test :)",
      "plain": "Test :)",
      "rich": "Test :)"
    },
    "thread_id": 857334186,
    "client_type": "Web",
    "client_url": "https://www.yammer.com/",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "en",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 0,
      "names": []
    },
    "content_excerpt": "Test :)"
  },
  {
    "id": 871550113,
    "sender_id": 1618001962,
    "replied_to_id": 857334186,
    "created_at": "2017/04/07 06:15:13 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/871550113",
    "web_url": "https://www.yammer.com/kcc.com/messages/871550113",
    "group_id": 10601015,
    "body": {
      "parsed": "QAhit",
      "plain": "QAhit",
      "rich": "QAhit"
    },
    "thread_id": 857334186,
    "client_type": "K-C Way of Brand Building",
    "client_url": "https://kc-way.com",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "en",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 0,
      "names": []
    },
    "content_excerpt": "QAhit"
  }
],
"857334359": [
  {
    "id": 870871366,
    "sender_id": 1621195723,
    "replied_to_id": 857334359,
    "created_at": "2017/04/06 05:19:14 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/870871366",
    "web_url": "https://www.yammer.com/kcc.com/messages/870871366",
    "group_id": 10601015,
    "body": {
      "parsed": "asas",
      "plain": "asas",
      "rich": "asas"
    },
    "thread_id": 857334359,
    "client_type": "K-C Way of Brand Building",
    "client_url": "https://kc-way.com",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "pt",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 0,
      "names": []
    },
    "content_excerpt": "asas"
  },
  {
    "id": 870476066,
    "sender_id": 1618001962,
    "replied_to_id": 857334359,
    "created_at": "2017/04/05 14:10:08 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/870476066",
    "web_url": "https://www.yammer.com/kcc.com/messages/870476066",
    "group_id": 10601015,
    "body": {
      "parsed": "test of gssmi",
      "plain": "test of gssmi",
      "rich": "test of gssmi"
    },
    "thread_id": 857334359,
    "client_type": "K-C Way of Brand Building",
    "client_url": "https://kc-way.com",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "en",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 0,
      "names": []
    },
    "content_excerpt": "test of gssmi"
  }
],
"861760787": [
  {
    "id": 861906344,
    "sender_id": 1520171236,
    "replied_to_id": 861760787,
    "created_at": "2017/03/20 01:23:41 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/861906344",
    "web_url": "https://www.yammer.com/kcc.com/messages/861906344",
    "group_id": 10218161,
    "body": {
      "parsed": "this is nice!",
      "plain": "this is nice!",
      "rich": "this is nice!"
    },
    "thread_id": 861760787,
    "client_type": "Web",
    "client_url": "https://www.yammer.com/",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "en",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 0,
      "names": []
    },
    "content_excerpt": "this is nice!"
  },
  {
    "id": 861769267,
    "sender_id": 1618001962,
    "replied_to_id": 861760787,
    "created_at": "2017/03/19 13:08:53 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/861769267",
    "web_url": "https://www.yammer.com/kcc.com/messages/861769267",
    "group_id": 10218161,
    "body": {
      "parsed": "opened cm",
      "plain": "opened cm",
      "rich": "opened cm"
    },
    "thread_id": 861760787,
    "client_type": "K-C Way of Brand Building",
    "client_url": "https://kc-way.com",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "en",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 0,
      "names": []
    },
    "content_excerpt": "opened cm"
  }
],
"862228666": [
  {
    "id": 863461826,
    "sender_id": 1527919854,
    "replied_to_id": 862228666,
    "created_at": "2017/03/22 18:32:36 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/863461826",
    "web_url": "https://www.yammer.com/kcc.com/messages/863461826",
    "group_id": 10218161,
    "body": {
      "parsed": "Looks great!",
      "plain": "Looks great!",
      "rich": "Looks great!"
    },
    "thread_id": 862228666,
    "client_type": "iPhone",
    "client_url": "https://about.yammer.com/product/mobile/ios/",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "en",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 1,
      "names": [
        {
          "full_name": "Karyn Prather (Yammer Admin)",
          "permalink": "karynlprather",
          "user_id": 1576848675,
          "network_id": 12467
        }
      ]
    },
    "content_excerpt": "Looks great!"
  },
  {
    "id": 863070068,
    "sender_id": 1618001962,
    "replied_to_id": 862228666,
    "created_at": "2017/03/22 03:20:18 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/863070068",
    "web_url": "https://www.yammer.com/kcc.com/messages/863070068",
    "group_id": 10218161,
    "body": {
      "parsed": "hi",
      "plain": "hi",
      "rich": "hi"
    },
    "thread_id": 862228666,
    "client_type": "K-C Way of Brand Building",
    "client_url": "https://kc-way.com",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "en",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 0,
      "names": []
    },
    "content_excerpt": "hi"
  }
],
"863094568": [
  {
    "id": 884958709,
    "sender_id": 1553423930,
    "replied_to_id": 863094568,
    "created_at": "2017/05/05 08:08:57 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/884958709",
    "web_url": "https://www.yammer.com/kcc.com/messages/884958709",
    "group_id": 10218161,
    "body": {
      "parsed": "test1",
      "plain": "test1",
      "rich": "test1"
    },
    "thread_id": 863094568,
    "client_type": "K-C Way of Brand Building",
    "client_url": "https://kc-way.com",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "en",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 0,
      "names": []
    },
    "content_excerpt": "test1"
  },
  {
    "id": 863094697,
    "sender_id": 1618001962,
    "replied_to_id": 863094568,
    "created_at": "2017/03/22 05:08:13 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/863094697",
    "web_url": "https://www.yammer.com/kcc.com/messages/863094697",
    "group_id": 10218161,
    "body": {
      "parsed": "testing",
      "plain": "testing",
      "rich": "testing"
    },
    "thread_id": 863094568,
    "client_type": "Web",
    "client_url": "https://www.yammer.com/",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "en",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 0,
      "names": []
    },
    "content_excerpt": "testing"
  }
],
"863094888": [
  {
    "id": 865647593,
    "sender_id": 1621195708,
    "replied_to_id": 863094888,
    "created_at": "2017/03/27 10:08:22 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/865647593",
    "web_url": "https://www.yammer.com/kcc.com/messages/865647593",
    "group_id": 10218161,
    "body": {
      "parsed": "hi test",
      "plain": "hi test",
      "rich": "hi test"
    },
    "thread_id": 863094888,
    "client_type": "Web",
    "client_url": "https://www.yammer.com/",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "en",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 0,
      "names": []
    },
    "content_excerpt": "hi test"
  },
  {
    "id": 863970308,
    "sender_id": 1621195708,
    "replied_to_id": 863094888,
    "created_at": "2017/03/23 13:30:37 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/863970308",
    "web_url": "https://www.yammer.com/kcc.com/messages/863970308",
    "group_id": 10218161,
    "body": {
      "parsed": "as",
      "plain": "as",
      "rich": "as"
    },
    "thread_id": 863094888,
    "client_type": "Web",
    "client_url": "https://www.yammer.com/",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "en",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 0,
      "names": []
    },
    "content_excerpt": "as"
  }
],
"864018272": [
  {
    "id": 884991311,
    "sender_id": 1553423930,
    "replied_to_id": 864018272,
    "created_at": "2017/05/05 09:26:43 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/884991311",
    "web_url": "https://www.yammer.com/kcc.com/messages/884991311",
    "group_id": 10218161,
    "body": {
      "parsed": "test",
      "plain": "test",
      "rich": "test"
    },
    "thread_id": 864018272,
    "client_type": "K-C Way of Brand Building",
    "client_url": "https://kc-way.com",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "en",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 0,
      "names": []
    },
    "content_excerpt": "test"
  },
  {
    "id": 864779303,
    "sender_id": 1618001962,
    "replied_to_id": 864018272,
    "created_at": "2017/03/24 17:02:22 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/864779303",
    "web_url": "https://www.yammer.com/kcc.com/messages/864779303",
    "group_id": 10218161,
    "body": {
      "parsed": "Hi",
      "plain": "Hi",
      "rich": "Hi"
    },
    "thread_id": 864018272,
    "client_type": "K-C Way of Brand Building",
    "client_url": "https://kc-way.com",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "nl",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 0,
      "names": []
    },
    "content_excerpt": "Hi"
  }
],
"864271024": [
  {
    "id": 887247097,
    "sender_id": 1621429355,
    "replied_to_id": 864271024,
    "created_at": "2017/05/10 13:35:29 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/887247097",
    "web_url": "https://www.yammer.com/kcc.com/messages/887247097",
    "group_id": 10218161,
    "body": {
      "parsed": "testing 567",
      "plain": "testing 567",
      "rich": "testing 567"
    },
    "thread_id": 864271024,
    "client_type": "K-C Way of Brand Building",
    "client_url": "https://kc-way.com",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "en",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 0,
      "names": []
    },
    "content_excerpt": "testing 567"
  },
  {
    "id": 864577480,
    "sender_id": 1618001962,
    "replied_to_id": 864271024,
    "created_at": "2017/03/24 10:54:12 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/864577480",
    "web_url": "https://www.yammer.com/kcc.com/messages/864577480",
    "group_id": 10218161,
    "body": {
      "parsed": "fghj",
      "plain": "fghj",
      "rich": "fghj"
    },
    "thread_id": 864271024,
    "client_type": "K-C Way of Brand Building",
    "client_url": "https://kc-way.com",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "en",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 0,
      "names": []
    },
    "content_excerpt": "fghj"
  }
],
"864742245": [
  {
    "id": 884920337,
    "sender_id": 1553423930,
    "replied_to_id": 864742245,
    "created_at": "2017/05/05 06:32:44 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/884920337",
    "web_url": "https://www.yammer.com/kcc.com/messages/884920337",
    "group_id": 10218161,
    "body": {
      "parsed": "test",
      "plain": "test",
      "rich": "test"
    },
    "thread_id": 864742245,
    "client_type": "K-C Way of Brand Building",
    "client_url": "https://kc-way.com",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "en",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 0,
      "names": []
    },
    "content_excerpt": "test"
  }
],
"864822591": [
  {
    "id": 884937275,
    "sender_id": 1553423930,
    "replied_to_id": 864822591,
    "created_at": "2017/05/05 07:16:42 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/884937275",
    "web_url": "https://www.yammer.com/kcc.com/messages/884937275",
    "group_id": 10218161,
    "body": {
      "parsed": "tst",
      "plain": "tst",
      "rich": "tst"
    },
    "thread_id": 864822591,
    "client_type": "K-C Way of Brand Building",
    "client_url": "https://kc-way.com",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "en",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 0,
      "names": []
    },
    "content_excerpt": "tst"
  },
  {
    "id": 869653071,
    "sender_id": 1618001962,
    "replied_to_id": 864822591,
    "created_at": "2017/04/04 06:49:34 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/869653071",
    "web_url": "https://www.yammer.com/kcc.com/messages/869653071",
    "group_id": 10218161,
    "body": {
      "parsed": "Hey",
      "plain": "Hey",
      "rich": "Hey"
    },
    "thread_id": 864822591,
    "client_type": "K-C Way of Brand Building",
    "client_url": "https://kc-way.com",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "en",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 0,
      "names": []
    },
    "content_excerpt": "Hey"
  }
],
"865755084": [
  {
    "id": 884921126,
    "sender_id": 1553423930,
    "replied_to_id": 865755084,
    "created_at": "2017/05/05 06:35:12 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/884921126",
    "web_url": "https://www.yammer.com/kcc.com/messages/884921126",
    "group_id": 10218161,
    "body": {
      "parsed": "test",
      "plain": "test",
      "rich": "test"
    },
    "thread_id": 865755084,
    "client_type": "K-C Way of Brand Building",
    "client_url": "https://kc-way.com",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "en",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 0,
      "names": []
    },
    "content_excerpt": "test"
  },
  {
    "id": 866548919,
    "sender_id": 1618001962,
    "replied_to_id": 865755084,
    "created_at": "2017/03/28 20:31:57 +0000",
    "network_id": 12467,
    "message_type": "update",
    "sender_type": "user",
    "url": "https://www.yammer.com/api/v1/messages/866548919",
    "web_url": "https://www.yammer.com/kcc.com/messages/866548919",
    "group_id": 10218161,
    "body": {
      "parsed": "test",
      "plain": "test",
      "rich": "test"
    },
    "thread_id": 865755084,
    "client_type": "K-C Way of Brand Building",
    "client_url": "https://kc-way.com",
    "system_message": false,
    "direct_message": false,
    "chat_client_sequence": null,
    "language": "en",
    "notified_user_ids": [],
    "privacy": "public",
    "attachments": [],
    "liked_by": {
      "count": 1,
      "names": [
        {
          "full_name": "Robert Visconti",
          "permalink": "robertvisconti",
          "user_id": 1527919854,
          "network_id": 12467
        }
      ]
    },
    "content_excerpt": "test"
  }
]
}}

请提供如何将此json转换为模型的解决方案。扩展线程中的所有对象都采用数字格式。选择所有json数据并复制它。然后在VisualStudio中遵循以下步骤

编辑>粘贴特殊>将JSON粘贴为类

然后所有需要的类将自动生成


但是请确保您的json格式正确。

好的,那么您需要链接在一起的两个解决方案是

  • 此堆栈溢出回答将帮助您创建以下类型的对象:
  • 如果您不知道动态对象上的属性是什么,那么您可以使用以下答案来迭代动态对象的属性,以获得属性的名称和/或它们的值
  • 您的问题有些不清楚,但我怀疑我看到了模式-
    threaded\u extended
    的值实际上是一个
    字典或类似的东西。这就是你应该如何表现它:

    public class Root
    {
        [JsonProperty("threaded_extended")]
        public Dictionary<int, List<Post>> ThreadedExtended { get; set; } 
    }
    
    public class Post
    {        
        [JsonProperty("id")]
        public int Id { get; set; }
        [JsonProperty("sender_id")]
        public int SenderId { get; set; }
        // etc
    }
    

    但是这里的物体是动态的,它会发生变化。我可以知道如何转换吗?这只是动态的第一部分吗?如果你能提供一些简单的JSON示例,这会有所帮助-我们不需要700行JSON,其中大部分都不是动态的。我知道如何反序列化它,但对象会像数字一样生成,它会动态地改变。这不是反序列化。这是从json数据创建模型。如果你的意思是每次你的模型可能会改变,那么你最好使用
    dynamic
    type并忘记模型类。这是一个很好的特性。我不知道这件事。谢谢然而,我不认为它回答了这个问题
    Root root = JsonConvert.DeserializeObject<Root>(json);