13

I have a List of class objects that have email address and status data members. I am trying to convert these to a json, making sure to have the "operations" word on the array.

This is my class:

class MyClass
{
    public string email {get; set; }
    public string status { get; set; }
}

This is my current code (not building):

List<MyClass> data = new List<MyClass>();
data = MagicallyGetData();

string json = new {
     operations = new {
          JsonConvert.SerializeObject(data.Select(s => new {
               email_address = s.email,
               status = s.status
          }))
     }
};

This is the JSON I am trying to get:

{
  "operations": [
    {
      "email_address": "[email protected]",
      "status": "good2go"
    },
    {
      "email_address": "[email protected]",
      "status": "good2go"
    },...
  ]
}

EDIT1 I should mention that the data I am getting for this comes from a DB. I am de-serializing a JSON from the DB and using the data in several different ways, so I cannot change the member names of my class.

4
  • what are you actually getting? Commented Feb 26, 2016 at 14:34
  • Why are your annoying yourself with anonymous objects ? Use datacontrat instead or name attribute newtonsoft.com/json/help/html/JsonPropertyName.htm Commented Feb 26, 2016 at 14:34
  • @ Arpit The closest I have gotten is the same output without the operations string before the array. Commented Feb 26, 2016 at 14:48
  • I assume this was with Json.NET from Newtonsoft Commented Nov 24, 2022 at 21:08

4 Answers 4

13

I believe this will give you what you want. You will have to change your class property names if possible.

Given this class

class MyClass
{
   public string email_address { get; set; }
   public string status { get; set; }
}

You can add the objects to a list

List<MyClass> data = new List<MyClass>()
{ 
   new MyClass(){email_address = "[email protected]", status = "s1"}
   , new MyClass(){ email_address = "[email protected]", status = "s1"}
};

Using an anonymous-type you can assign data to the property operations

var json = JsonConvert.SerializeObject(new
{
   operations = data
});
Sign up to request clarification or add additional context in comments.

3 Comments

With a few tweaks, this is the solution I went with. Thanks, appreciate it.
What, if I don't want either data or operations at the beginning?
@HafizH if you don't want "operations" at the beginning simply do: var json = JsonConvert.SerializeObject(operations)
4
class MyClass
{
     public string email_address { get; set; }
     public string status { get; set; }
}

List<MyClass> data = new List<MyClass>() { new MyClass() { email_address = "[email protected]", status = "good2go" }, new MyClass() { email_address = "[email protected]", status = "good2go" } };

//Serialize
var json = JsonConvert.SerializeObject(data);

//Deserialize
var jsonToList = JsonConvert.DeserializeObject<List<MyClass>>(json);

Comments

0

You can try with something like this:

using System.Web.Script.Serialization;
var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(data);

Comments

-1

Here is the simple code

JArray.FromObject(objList);

1 Comment

To be able to understand your answer more details are needed. Currently it looks to me that this will not work.

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.