【问题标题】:How can I get this base = { base: 'base', prop: '2093482938' } array in JavaScript to C#?如何将 JavaScript 中的这个 base = { base: 'base', prop: '2093482938' } 数组转换为 C#?
【发布时间】:2021-05-20 02:01:42
【问题描述】:

base = { base: 'base', prop: '897070980989'}; ingr_mapArray.push(base); protien = { protein: 'protein', prop: '452341342453'}; ingr_mapArray.puh(protein);

我想在 C# 中获取 ingr_mapArray。并希望存储在 List 中。我怎样才能得到这个数组?

【问题讨论】:

  • 对于List<'Ingrediants>,您需要类Ingrediants。 base 和 protien 对象都有不同的字段。为了使它成为一个类,他们需要相同的字段

标签: javascript c# asp.net-mvc web-applications


【解决方案1】:

你应该这样做

class Ingrediants
{
    string Type { get; set; }
    string Prop { get; set; }
}

那么你需要在哪里做的列表

List<Ingrediants> ingr_mapArray = new List<Ingrediants>();

Ingrediants base = new Ingrediants
{
   Type = "base",
   Prop = "897070980989"
};

ingr_mapArray.Add(base);

Ingrediants protien = new Ingrediants
{
   Type = "protien",
   Prop = "452341342453"
};

ingr_mapArray.Add(protien);

在您的示例中,base 对象具有属性 "base",类似地,protien 具有 "protien",这不是您可以在同一类中执行的操作。所以我将其命名为Type,您可以在其中提供baseprotien

【讨论】:

    猜你喜欢
    • 2012-03-22
    • 1970-01-01
    • 2014-11-22
    • 2019-10-09
    • 2017-12-11
    • 2022-10-23
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多