Unchained Unchained - 11 months ago 48
JSON Question

How to create a json with different index?

I'm using

for build my json file, actually what I did is create different classes models, as this:

class GeneralModel
public string Language { get; set; }

and then for build the json file:

GeneralModel lsModel = new GeneralModel();
string json = JsonConvert.SerializeObject(lsModel);
File.WriteAllText(settingsFilePath, json);

this will create a json that contains
, but I need to put in this json different classes, what I need is set an index that contains each class name, for example:

"GeneralModel": {
"Language" : "english"
"AnoherClassName": {
"ClassProperty" : value

how can I do this with Newtonsoft?

Answer Source
 public class GeneralModel
    public string Language { get; set; }

public class AnotherModel
    public string AnotherProperty { get; set; }

public class SuperClass
    public GeneralModel generalModel { get; set; }

    public AnotherModel anotherModel { get; set; }


            SuperClass s = new WMITest.SuperClass();
            s.generalModel = new GeneralModel();
            s.generalModel.Language = "language";
            s.anotherModel = new AnotherModel();
              s.anotherModel.AnotherProperty = "example";
            string json = JsonConvert.SerializeObject(s);