在C#中将变量注入json数组[已关闭]

ctehm74n  于 2023-01-22  发布在  C#
关注(0)|答案(2)|浏览(139)

十小时前关门了。
Improve this question
你好,我有一个这样的json:

{
    "fields": {
        "project": {
            "key": "Proj"
        },
        "summary": "test",
       "description": "h2",
        "components": [
            {
                "name": "AXX1"
            }
        ],
        "issuetype": {
            "name": "Problem"
        }
    }
}

我的json里面有一个数组,描述、组件和issuetype是我注入的变量:

public void Main()
        {
             

                    String Summary = (String)Dts.Variables["User::Summary"].Value;
                    String IssueType = (String)Dts.Variables["User::IssueType"].Value;
                    String Description = (String)Dts.Variables["User::Description"].Value;
                    String Project = (String)Dts.Variables["User::Project"].Value;
                    String Components = (String)Dts.Variables["User::Components"].Value;

            try
                {

               
                    var data = new Issue();
                    data.Fields.Project.Key = Project;
                    data.Fields.Summary = Summary;
                    data.Fields.Description = Description;
                    data.Fields.IssueType.Name = IssueType;
                    data.Fields.Components[0].Name = Components;

                string json = JsonConvert.SerializeObject(data);

                

        public class Issue
        {
            [JsonProperty("fields")]
            public Fields Fields
            {
                get;
                set;
            }
            public Issue()
            {
                Fields = new Fields();
            }
        }

        public class Fields
        {
            [JsonProperty("project")]
            public Project Project
            {
                get;
                set;
            }
            [JsonProperty("summary")]
            public string Summary
            {
                get;
                set;
            }
            [JsonProperty("description")]
             public string Description
             {
                 get;
                 set;
             }
            
            [JsonProperty("Components")]
           public List<Components> Components { get; set; }
             

            [JsonProperty("issuetype")]
            public IssueType IssueType
            {
                get;
                set;
            }
            public Fields()
            {
                Project = new Project();
                IssueType = new IssueType();
            }
        }

        public class Project
        {
            [JsonProperty("key")]
            public string Key
            {
                get;
                set;
            }
        }

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

我创建了如下的类:
但现在我在将变量注入组件时遇到错误。
我新增数据.字段.组件[0].名称=组件;并且该名称用红色下划线表示它是无效的。
一旦这是一个数组,我如何注入它?

"components": [
            {
                "name": "post"
            }
        ],
zdwk9cvp

zdwk9cvp1#

你的代码一点都不清楚,你把示例和示例的属性混在一起了,但是我认为你需要这段代码

if(data.Fields.Components==null) data.Fields.Components=new List<Componets>();

data.Fields.Components.Add ( new Components {Name = "The new name"});

//or if String Components = (String)Dts.Variables["User::Components"].Value is a name, not a component

if(data.Fields.Components==null) data.Fields.Components=new List<Componets>();
data.Fields.Components.Add ( new Components {Name = Components});

// or if it is a Components instance

data.Fields.Components.Add ( Components);
e7arh2l6

e7arh2l62#

在编写C#时,必须使用C#属性的名称,而不是JSON属性的名称。因此,应该设置Name而不是[name]
但是,当我认为您实际上想写入Component时,您也试图写入List<Component>的属性。您可以通过在方括号([])中指定要更改的组件的索引来完成此操作,因此更改列表中第一个ComponentName属性将类似于以下内容:

data.Fields.Components[0].Name = "The new name";

当然,这是假设data.Fieldsd.Components有值的情况下,查看您的代码,变量Components(您试图赋给[name]属性的变量)可能首先就是List<Component>

data.Fields.Components = Components;
data.Fields.Components[0].Name = "The new name";

(or或者)

Components[0].Name = "The new name";
data.Fields.Components = Components;

相关问题