Home > OS >  Inject variable into json array in C#
Inject variable into json array in C#

Time:01-20

Hello I have a json like this:

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

My json has an array inside and the description, components and issuetype are variables where I inject:



 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;
            }
        }

I have created classes like this:

But now I am getting error in injecting the variable into components.

I add data.Fields.Components[0].Name = Components; and the name is underlined in red and saying it is not valid.

How Can I inject this once this is an array?

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

CodePudding user response:

You code is not clear at all, you are mixing the instance with the property of the instance, but i think you need this code

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);

CodePudding user response:

When you are writing the C#, you have to use the name of the C# property and not the name of the JSON property. So, you should be setting Name instead of [name].

However, you are also trying to write to the property of a List<Component> when I think you actually want to write to a Component. You can do this by specifying the index of the component you wish to change in square brackets ([]), so to change the Name property of the first Component in the list would be something like the following:

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

Of course, this is assuming that data.Fieldsd.Components has values. Looking at your code, it could be that the variable Components (that you are trying to assign to the [name] property) is the List<Component> in the first place. In that case, you may need to do something like this instead

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

(or alternatively)

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