简体   繁体   中英

Create complex JSON using C# object

I am trying to create the json from c# object which holds the data. Data is in simple table format. Consist of Columns: Name, InputCode, DisplayID, CodeID, ParentID. If Parent(Net) then ParentID is null. If Children(Subnet), has ParentID that holds CodeID of Parent(Net). CodeID is Unique. I am facing the issue with iternation under subnet using foreach. It dosen't allow.

public class CodeFrameJson
    {
        public string Name { get; set; }
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public int? InputCodeValue { get; set; }
        public int DisplayId { get; set; }
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public List<CodeFrameJson> Subnet { get; set; }
    }
List<CodeFrameJson> cfj = new List<CodeFrameJson>();
            IEnumerable<CodeDTO> _cfd = new List<CodeDTO>();
            _cfd = codeFrameJson.GetCodeFrame(questionId, projectName);
            _cfd = _cfd.OrderBy(a => a.DisplayOrderNo).ToList();
            foreach (var a in _cfd)
            {
                int CodesID = 0;
                CodeFrameJson obj = new CodeFrameJson();
                if (a.InputCodeValue == null)
                {
                    var root = new CodeFrameJson()
                    {
                        Name = a.CodeName,
                        DisplayId = a.DisplayOrderNo,
                        InputCodeValue = null,
                        Subnet = new List<CodeFrameJson>()
                        {
                            //Start: Not allowing foreach
                            foreach (var x in _cfd)
                            {
                                if (x.ParentId == CodesID)
                                {
                                    new CodeFrameJson()
                                    {
                                        Name = x.CodeName,
                                        InputCodeValue = x.InputCodeValue,
                                        DisplayId = x.DisplayOrderNo
                                    };
                                }
                            }
                    //End: Not allowing foreach
                }
            };
                    obj = root;
                }
                else {
                    var root = new CodeFrameJson()
                    {
                        Name = a.CodeName,
                        DisplayId = a.DisplayOrderNo,
                        InputCodeValue = a.InputCodeValue,
                        Subnet = null
                    };
                    obj = root;
                }
                cfj.Add(obj);
            }
            var json = JsonConvert.SerializeObject(cfj, Formatting.Indented);

Final Output Something like this which can be distinguished easily

{
  "Site": {
    "Name": "Site",
    "DisplayID": 1,
    "Subnet": [
      {
        "Name": "Full Site",
        "InputCodeValue": 1,
        "DisplayId": 2
      },
      {
        "Name": "Partial Site",
        "InputCodeValue": 2,
        "DisplayId": 3
      }
    ]
  },
  "Test": {
    "Name": "Test1",
    "InputCodeValue": 3,
    "DisplayId": 4
  }
}

This doesn't really have to do anything with JSON, but with object (collection) initialization. You can only assign values there, but LinQ comes to the rescue:

Simply filter your list and create the new objects in the select statement:

Subnet = _cfd.Where(x => x.ParentId == CodesID).Select(x => new CodeFrameJson
{
    Name = x.CodeName,
    InputCodeValue = x.InputCodeValue,
    DisplayId = x.DisplayOrderNo
}).ToList()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM