4

I looked at this Create tree hierarchy in JSON with LINQ question and answer and it's close to what I need. However I can't generate the nested array as shown below as I am using json.net

Class.cs

    public class RootObject
    {
        public List<Attribute> test { get; set; }
    }
    public class Attribute
    {
        public string testKey { get; set; }
        public string start { get; set; }
        public string finish { get; set; }
        public string comment { get; set; }
        public string status { get; set; }
    }


    public class AttributeData
    {
        public Attribute AttributeDataOps()
        {
            ***Attribute DATA***
        }
    }

Program.cs

Attribute attribute = new Attribute();
AttributeData attributeData = new AttributeData();
string JSONresult = JsonConvert.SerializeObject(attribute);
string path = @"C:\file.json";

Expected Result

{
    "tests" : [
        {
            "testKey" : "",
            "start" : "",
            "finish" : "",
            "comment" : "",
            "status" : ""
        }
    ]
}

Current Result

    {
        "testKey" : "",
        "start" : "",
        "finish" : "",
        "comment" : "",
        "status" : ""
    }

1 Answer 1

4

Your expecting a collection but your passing a single object to serialize.

var rootObject = new RootObject();
List<Attribute> attribute = new List<Attribute>();
rootObject.test = attribute;
string JSONresult = JsonConvert.SerializeObject(rootObject);
Sign up to request clarification or add additional context in comments.

2 Comments

the list<attribute> portion cannot be shown. Do I need to create a object for that?
You need to add value to it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.