【问题标题】:Convert array of key objects pair to a C# object将键对象对数组转换为 C# 对象
【发布时间】:2022-01-18 09:04:04
【问题描述】:

我有一个以这种格式返回数据的 api

 { "reult": 
   { "70": 
     { 
       "type_id": 3, 
       "type": "forex", 
       "group_title": "forex", 
       "name": "EUR", 
       "title": "EUR" 
     }, 
     "724": 
      { 
        "type_id": 5, 
        "type": "shares", 
        "group_title": "shares", 
        "name": "#ABT", 
        "title": "#ABT" 
     } 
   }
 } 

现在我希望将这些关键对象对数据转换为真正的 C# 对象数组。像这样

 [
  {
    Id = 70
    Type_id = 3,
    Type = "forex",
    Group_title = "forex",
    Name = "EUR",
    Title = "EUR"
  },
  {
    Id = 724,
    Type_id = 5,
    Type = "shares",
    Group_title = "shares",
    Name = "#ABT",
    Title = "#ABT"
  }
]

有可能吗? [我已经更新了api返回的数据。因为使用这种格式很容易用 c# Dictionary 对这些数据进行除毒]

【问题讨论】:

  • 最上面的不是有效的 JSON 数据。您确定这是 API 返回数据的方式吗?
  • 是的,我只是删除了结果属性 { "reult": { "70": { "type_id": 3, "type": "forex", "group_title": "forex", "name": "EUR", "title": "EUR" }, "724": { "type_id": 5, "type": "shares", "group_title": "shares", "name": "# ABT", "title": "#ABT" } }}
  • 您在寻找 JavaScript 还是 C# 代码?
  • @Steve 寻找 C# 代码
  • 明白了。该帖子使用 javascript 和 C# 标记。所以,我不确定

标签: javascript c# arrays .net object


【解决方案1】:

假设你有一些课程:

public class Root
{
    [JsonProperty("result")]
    public Dictionary<int, Data> Result {get;set;}
}

public class Data
{
    [JsonIgnore]
    public int Id { get; set; }

    [JsonProperty("type_id")]
    public int TypeId { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("group_title")]
    public string GroupTitle { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("title")]
    public string Title { get; set; }
}

如果你想从中得到一个数组,你可以保留原始数据(不是你通过删除“结果”修改的数据)然后调用:

var root = JsonConvert.DeserializeObject<Root>(str);
foreach(var kvp in root.Result) kvp.Value.Id = kvp.Key;
var arr = root.Result.Values.ToArray();

ps;您需要参考 Newtonsoft.Json; System.Collections.Generic; System.Linq;

【讨论】:

  • 不是我想要的,我试过了,但你知道这不是一个对象 "70": { "type_id": 3, "type": "forex", "group_title": "forex" , "name": "EUR", "title": "EUR" } 如果格式是这样的,那么您的代码将起作用。 { "70": { "type_id": 3, "type": "forex", "group_title": "forex", "name": "EUR", "title": "EUR" } }
  • 我确实说过原始数据,即您在评论中所说的:是的,我只是删除了结果属性它是{“reult”:{“70 ": { "type_id": 3, "type": "forex", "group_title": "forex", "name": "EUR", "title": "EUR" }, "724": { "type_id" : 5, "type": "shares", "group_title": "shares", "name": "#ABT", "title": "#ABT" } }} - 不要删除“结果”属性;它会损坏您的 json 并使其无效。如果您已经将其删除,例如手动保存文件后再也无法获取,则需要手动添加回来
  • 代码工作正常:dotnetfiddle.net/nKayIb
  • 好的,非常感谢。它有效,问题在于删除结果。
  • 没问题!关于如何使用堆栈溢出的快速教程:当有人写出您认为有用或有帮助的答案时,请单击数字上方的向上箭头?。当有人写下您最终用来解决问题的答案时,请单击灰色复选 ☑️ 将其变为绿色 ✅。您可以在许多答案上单击?,包括您检查的答案,但只能检查一个答案✅ - 当您这样做时,它会更改仪表板上问题的外观,因此人们知道您不是仍在寻找答案
【解决方案2】:

使用这些属性创建类 然后使用下面的代码:

JsonSerializer.Deserialize<List<object>>(jsonString);

这里是更详细的链接:

https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-6-0

【讨论】:

  • 这不是正确的答案。每个对象都有一个 id 作为键,因此您的答案将不起作用。
【解决方案3】:

当我将你的 json 反序列化为 c# 类时 它是正确的类对象 也许你的 json 不正确

它是 c# 类:

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
    public class _70
    {
        public int type_id { get; set; }
        public string type { get; set; }
        public string group_title { get; set; }
        public string name { get; set; }
        public string title { get; set; }
    }

    public class _724
    {
        public int type_id { get; set; }
        public string type { get; set; }
        public string group_title { get; set; }
        public string name { get; set; }
        public string title { get; set; }
    }

    public class Reult
    {
        public _70 _70 { get; set; }
        public _724 _724 { get; set; }
    }

    public class Root
    {
        public Reult reult { get; set; }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 2020-06-15
    • 2016-03-11
    • 1970-01-01
    • 2021-02-14
    相关资源
    最近更新 更多