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

时间:2017-05-12 04:58:35

标签: c# json json.net

如果该对象是数字并且我想访问该对象的字段,我需要将json对象转换为模型的解决方案。

{"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转换为模型,threads_extended中的所有对象都是数字格式

3 个答案:

答案 0 :(得分:1)

选择所有Json数据并复制它。然后在Visual Studio中按照

进行操作
  

编辑>选择性粘贴>将JSON粘贴为类

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

enter image description here

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

答案 1 :(得分:1)

您的问题有些不清楚,但我怀疑我看到了模式 - threaded_extended的值有效地 a Dictionary<int, List<Post>>或类似。这就是你应该如何表现它:

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反序列化为Root

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

答案 2 :(得分:0)

好的,你需要连接在一起的两个解决方案是......

  1. 此堆栈溢出答案将帮助您创建dynamic类型的对象:
  2. Deserialize json object into dynamic object using Json.net

    1. 如果您不知道动态对象的属性是什么,那么您可以使用以下答案迭代动态对象的属性以获取属性和/或其值的名称
    2. How do I iterate over the properties of an anonymous object in C#?