0

Sorry if this has been asked already, suppose I have an anonymous list of objects like this:

   Collection = new List<object>() {
    new {
        FirstSetting    =  new
        {
            FirstKEy = "Property vs Inspections",
            color = "#FF6384",
            fontStyle = "Arial",
            sidePadding = 10,
        }
    },
    new {
        AnotherSettings =  new
        {
            text = "another text",
            color = "#FF6384",
            fontStyle = "Arial",
            sidePadding = 10,
        }
    }
};

and would like to convert to a JSON string using

JsonConvert.SerializeObject(Collection, JsonSerializerSettings);

I get this result.

[{
    "FirstSetting": {
        "FirstKey": "Property vs Inspections",
        "color": "#FF6384",
        "fontStyle": "Arial",
        "sidePadding": 10
    }
}, {
    "anotherSettings": {
        "text": "another text",
        "color": "#FF6384",
        "fontStyle": "Arial",
        "sidePadding": 10
    }
}]

but I do not wish to have an array. I'd like to have each settings to be an object something like this.

{
    "FirstSetting": {
        "FirstKey": "Property vs Inspections",
        "color": "#FF6384",
        "fontStyle": "Arial",
        "sidePadding": 10
    },
    "anotherSettings": {
        "text": "another text",
        "color": "#FF6384",
        "fontStyle": "Arial",
        "sidePadding": 10
    }
}

could someone shed some light please?

2
  • Collections will be serialized as arrays in JSON. Commented Apr 13, 2018 at 10:22
  • 1
    You have an anonymous object inside of an anonymous object which are both going to be viewed as arrays. You can write your own Json parsing method using reflection and just parse it yourself or try a different approach. Maybe place them in a Dictionary<string, object> and let the key be the names instead of using anonymous objects for that. Commented Apr 13, 2018 at 10:31

3 Answers 3

2

Not that's it's any better but if anyone finds it useful you can use a dictionary for this also. It's still clean code and it removes the first object 'Result' from the list. I sort of find it cleaner but that's preference.

var collection = new Dictionary<object, object>()
{
    ["FirstSetting"] = new
    {
        FirstKEy = "Property vs Inspections",
        color = "#FF6384",
        fontStyle = "Arial",
        sidePadding = 10,
    },
    ["AnotherSettings"] = new
    {
        text = "another text",
        color = "#FF6384",
        fontStyle = "Arial",
        sidePadding = 10,
    }
};

var jsonString = JsonConvert.SerializeObject(collection);

It outputs to:

{
    "FirstSetting": 
    { 
        "FirstKEy":"Property vs Inspections",
        "color":"#FF6384",
        "fontStyle":"Arial",
        "sidePadding":10
    },
    "AnotherSettings":
    {
        "text":"another text",
        "color":"#FF6384",
        "fontStyle":"Arial",
        "sidePadding":10
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

For that you need to construct another anonymous object in that way to get the desired json.

If you serialize the collection then it will obviously be converted to array in json :

var obj = new
    {
        Result = new
        {
            FirstSetting = new
            {
                FirstKEy = "Property vs Inspections", color = "#FF6384", fontStyle = "Arial", sidePadding = 10
            }, 
            AnotherSettings = new
            {
                text = "another text", color = "#FF6384", fontStyle = "Arial", sidePadding = 10 
            }
        }
    };

The json generated would be :

{
   "Result":{
      "FirstSetting":{
         "FirstKey":"Property vs Inspections",
         "color":"#FF6384",
         "fontStyle":"Arial",
         "sidePadding":10
      },
      "anotherSettings":{
         "text":"another text",
         "color":"#FF6384",
         "fontStyle":"Arial",
         "sidePadding":10
      }
   }
}

Comments

1

To follow up on the answer Ehsan Sajjad. Putting it in an object makes it a bit more readable I think.

Object mySettings = new {
    FirstSetting = new {
            FirstKEy = "Property vs Inspections",
            color = "#FF6384",
            fontStyle = "Arial",
            sidePadding = 10,
    },
    AnotherSettings = new {
        text = "another text",
        color = "#FF6384",
        fontStyle = "Arial",
        sidePadding = 10,
    }
};

The above might give you the desired result.

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.