Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C#对象创建复杂的JSON_C#_Json.net - Fatal编程技术网

使用C#对象创建复杂的JSON

使用C#对象创建复杂的JSON,c#,json.net,C#,Json.net,我试图从保存数据的c#对象创建json。 数据采用简单的表格格式。由列组成:Name、InputCode、DisplayID、CodeID、ParentID。 如果父项(净),则ParentID为空。如果子项(子网),则具有包含父项(网络)代码ID的ParentID。 CodeID是唯一的。 我正面临使用foreach的子网下的iternation问题。这是不允许的 public class CodeFrameJson { public string Name { get

我试图从保存数据的c#对象创建json。 数据采用简单的表格格式。由列组成:Name、InputCode、DisplayID、CodeID、ParentID。 如果父项(净),则ParentID为空。如果子项(子网),则具有包含父项(网络)代码ID的ParentID。 CodeID是唯一的。 我正面临使用foreach的子网下的iternation问题。这是不允许的

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; }
    }

这实际上与JSON无关,而是与对象(集合)初始化有关。您只能在此处赋值,但LinQ起到了帮助作用:

只需过滤列表并在select语句中创建新对象:

Subnet=\u cfd.Where(x=>x.ParentId==CodesID)。选择(x=>newcodeframeJSON
{
Name=x.CodeName,
InputCodeValue=x.InputCodeValue,
DisplayId=x.DisplayOrderNo
})托利斯先生()

您的问题是什么?您想知道如何使用集合初始化器吗?
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);
{
  "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
  }
}