0

I have a code that generate Json string .

public class Work
{
    public string id { get; set; }
    public string name { get; set; }
    public bool status { get; set; }
    public bool open { get; set; }
   
}
public class Root
{
    public IList<Work> work { get; set; }    
}


public void Work_add(string id, string nameM)
    {                    
        _work.Add(new Work()
        {
            id = id,
            name = nameM,
            status = false,
            open = false
        });
    }
    
public List<Work> _work = new List<Work>(); 

public void Print_Json()
{
.
.
string jsonE = JsonConvert.SerializeObject( _work);
}

When a list is populated and then serialised I get this JSON:

[{"id":"1","name":"AAA","status":"false","open":"false"},{"id":"2","name":"BBB","status":"false","open":"false"},{"id":"4","name":"CCC","status":"false","open":"false"},{"id":"5","name":"DDD","status":"false","open":"false"},{"id":"6","name":"EEE","status":"false","open":"false"},{"id":"7","name":"FFF","status":"false","open":"false"},{"id":"8","name":"GGG","status":"false","open":"false"}]

I looked in this topic and in other solutions, but I'm missing somethings....

I need add "work" name before the list with {,}

{"work":[{"id":"1","name":"AAA","status":"false","open":"false"},{"id":"2","name":"BBB","status":"false","open":"false"},{"id":"4","name":"CCC","status":"false","open":"false"},{"id":"5","name":"DDD","status":"false","open":"false"},{"id":"6","name":"EEE","status":"false","open":"false"},{"id":"7","name":"FFF","status":"false","open":"false"},{"id":"8","name":"GGG","status":"false","open":"false"}]}
4
  • Is Print_Json a member of the Root class? If yes, do JsonConvert.SerializeObject(this); instead Commented Jan 10, 2023 at 16:54
  • No, It's independent functions Commented Jan 10, 2023 at 16:57
  • So were does work come from in that method? Please provide reproducible code Commented Jan 10, 2023 at 16:59
  • That is a problem, I don't have enough IQ how to implement it. I understand that I need link _work with the Root class and call it in JsonConvert Commented Jan 10, 2023 at 17:04

1 Answer 1

1

You should do the following.

public void Print_Json()
{
  var root = new Root();
  root.work = _work;
  string jsonE = JsonConvert.SerializeObject(root);
}
Sign up to request clarification or add additional context in comments.

1 Comment

A thousand Thanks!!!! Works!

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.