如何反序列化Json,类名为_1330789870659362817,且总是随新类名变化[重复]

gudnpqoy  于 2023-01-03  发布在  其他
关注(0)|答案(1)|浏览(81)
    • 此问题在此处已有答案**:

Deserialize json object with dynamic items in C#(1个答案)
Using JSON.NET to read a dynamic property name(1个答案)
Complicated Json to C# Object Deserialize with classes(2个答案)
3天前关闭.

"user": {
  "1330789870659362817": {
    "id": "xxxxxxxxxxxxxxxxxx",
    "created_timestamp": "1606120016282",
    "name": "yyyyyyyyyyyyy",
    "screen_name": "wwwwwwwww",
    "protected": false,
    "verified": false,
    "followers_count": 9,
    "friends_count": 23,
    "statuses_count": 368,
    "profile_image_url": "qqqqqqqqqqqqqqqq",
    "profile_image_url_https": "sssssssss"
  },
  "1467799120933003275": {
    "id": "wwwwwwwww",
    "created_timestamp": "1638785572496",
    "name": "cccccccccccc",
    "screen_name": "xxxxxxxxxxxx",
    "location": "xxxxxxxx",
    "description": "Cricket",
    "protected": false,
    "verified": false,
    "followers_count": 1,
    "friends_count": 4,
    "statuses_count": 37,
    "profile_image_url": "xxxxxx",
    "profile_image_url_https": "xxxxxxxxx"
  }
}

我希望使用类反序列化该json,当另一个请求带有其他编号(如_5838364784684847657)时,每个请求都应可接受

x4shl7ld

x4shl7ld1#

可以反序列化为RootObject对象,该对象将字典作为成员。

public class User
{
    public string id { get; set; }
    public string created_timestamp { get; set; }
    public string name { get; set; }
    public string screen_name { get; set; }
    public string location { get; set; }
    public string description { get; set; }
    public bool @protected { get; set; }
    public bool verified { get; set; }
    public int followers_count { get; set; }
    public int friends_count { get; set; }
    public int statuses_count { get; set; }
    public string profile_image_url { get; set; }
    public string profile_image_url_https { get; set; }
}

public class RootObject
{
    public Dictionary<string, User> user { get; set; }
}

我使用NewtonSoft.Json进行反序列化。

var obj = JsonConvert.DeserializeObject<RootObject>(json);

foreach (var kvp in obj.user)
{
    string key = kvp.Key;
    User user = kvp.Value;
}

相关问题