【问题标题】:Create Json structure using JsonConvert.SerializeObject使用 JsonConvert.SerializeObject 创建 Json 结构
【发布时间】:2017-04-23 13:14:27
【问题描述】:

我的模型类如下:

public class Details
    {
        public Customer customer{ get; set; }
        public List<Point> points{ get; set; }
        public List<Color> colors { get; set; }

    }

这是我的模型的填充方式:-

    Details details = new Details();

     var info = gets data from service
                    foreach (var item in info)
                    {
                        Points p = new Points();
                        p.value = item.value;
                       details.points= new List<Dial>();

                        fusionChartDetails.points.Add(p);
                    }

                    Color c1 = new Color();
                    c1.code = "#1";
                    Color c2 = new Color();
                    c2.code = "#2";
                    Color c3 = new Color();
                    c3.code = "#3";
                    details.colors = new List<Color>();
                    details.colors.Add(c1);

                    details.colors.Add(c2);

                    details.colors.Add(c3);
}

  var final=JsonConvert.SerializeObject<details>();

预期:json

 {
     "customer": {
         "caption": "Customer 1"

     },
     "colors": {
         "color": [
             {

                 "code": "#1"
             },
             {

                 "code": "#2"
             },
             {

                 "code": "#3"
             }
         ]
     },
     "points": {
         "point": [
             {
                 "value": "10"
             }
         ]
     } }

但是得到这个:

{
    "customer": {
        "caption": "Customer 1" 
    },
    "colors": [
        {

            "code": "#1"
        },
        {

            "code": "#2"
        },
        {

            "code": "#3"
        }
    ],
    "points": [
        {
            "value": "10"
        }
    ]
}

不同的是,我的 json 结构中没有构建内部点和颜色对象。如何做到这一点?

【问题讨论】:

    标签: c# json asp.net-mvc serialization json.net


    【解决方案1】:

    您将需要几个额外的课程:

    public class Details
    {
        public Customer customer { get; set; }
        public Points points { get; set; }
        public Colors colors { get; set; }
    }
    
    public class Points
    {
        public List<Point> point { get; set; }
    }
    
    public class Color
    {
        public List<Color> color { get; set; }
    }
    

    然后像这样填充:

    details.points = new Points
    {
        point = new List<Point>,
    };
    details.points.point.add(p1);
    details.points.point.add(p2);
    details.points.point.add(p3);
    
    ...
    
    details.colors = new Colors
    {
        color = new List<Color>(),
    };
    details.colors.color.Add(c1);
    details.colors.color.Add(c2);
    details.colors.color.Add(c3);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-05
      • 2019-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多