1

I want to get JSON similar to this

{
"Name": "xxxxx",
"ApplicationId": "xxxxx",
"Features": [
  {
     "Name": "xxxxx",
     "Code": "xxxxx",
  }
 ]
}

and my code is so far is

var json = JsonConvert.SerializeObject(
       new {
            Name = name ,
            ApplicationId = applicationID ,
            Features = (new[]{feature_name , feature_code})
           }, Formatting.None);

I can not figure out the feature part, it won't get created as in the example. I read so many post, but did not find anything about inner objects.

0

2 Answers 2

2

You didn't give the features appropriate names, hence then variable names are used.

Use this instead:

new[] { new { Name = feature_name, Code = feature_code } }

Also, you have to supply a list of features, for example using LINQ.

features.Select(f => new { Name = f.feature_name, Code = f.feature_code })
Sign up to request clarification or add additional context in comments.

Comments

1

Another way of doing this can be like:

var createJson = new {
    Name = "xxxxx",
    ApplicationId = "xxxxx",
    Features = new[] {
      new  { Name = "xxxxx", Code = "xxxxx" },
      new  { Name = "xxxxx", Code = "xxxxx" }
    }
};
var json = JsonConvert.SerializeObject(createjson, Formatting.Indented);

Comments

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.