C#按键值对JSON分组

时间:2018-08-29 17:26:31

标签: c# json serialization

社区 我有一个存储过程,它返回此JSON作为响应:

[
  {
    "displayorder": 2,
    "oppItemID": 4,
    "opportunityName": "Net 10",
    "title": "New Amazing Promotion",
    "description": "Amazing",
    "image": "imagelink",
    "opDate": "2018-08-09T22:00:00.937",
    "slideOrder": 0,
    "status": false,
    "isCover": false
  },
  {
    "displayorder": 1,
    "oppItemID": 2,
    "opportunityName": "Simple Mobile",
    "title": "New promo",
    "description": "Amazing",
    "image": "imagelink",
    "opDate": "2018-08-09T22:00:00.937",
    "slideOrder": 0,
    "status": false,
    "isCover": false
  },
  {
    "displayorder": 1,
    "oppItemID": 3,
    "opportunityName": "Simple Mobile",
    "title": "New Amazing Promotion",
    "description": "Amazing",
    "image": "imagelink",
    "opDate": "2018-08-10T22:00:00.937",
    "slideOrder": 0,
    "status": false,
    "isCover": false
  },
  {
    "displayorder": 8,
    "oppItemID": 5,
    "opportunityName": "Verizon",
    "title": "New Amazing Promotion",
    "description": "Amazing",
    "image": "imagelink",
    "opDate": "2018-08-09T22:00:00.937",
    "slideOrder": 0,
    "status": false,
    "isCover": false
  },
  {
    "displayorder": 8,
    "oppItemID": 27,
    "opportunityName": "Verizon",
    "title": "New Amazing",
    "description": "Amazing",
    "image": "imagelink",
    "opDate": "2018-08-22T22:00:00.937",
    "slideOrder": 0,
    "status": false,
    "isCover": false
  }
]

如您所见,有多个具有相同“ opportunityName”的对象

我想要做的是按特定的“ opportunityName”对它们进行分组,并具有对象列表,即:

"Varizon":{
     0:{
        "displayorder": 8,
        "oppItemID": 5,
        "title": "New Amazing Promotion",
        "description": "Amazing",
        "image": "imagelink",
        "opDate": "2018-08-09T22:00:00.937",
        "slideOrder": 0,
        "status": false,
        "isCover": false
      },
      1:{
        "displayorder": 8,
        "oppItemID": 27,
        "title": "New Amazing",
        "description": "Amazing",
        "image": "imagelink",
        "opDate": "2018-08-22T22:00:00.937",
        "slideOrder": 0,
        "status": false,
        "isCover": false
      }
}

其余的商机名称也一样

这是代码:

public List<OpportunitiesByNameForDisplay> GetBdmOpportunites(string rswnum)
        {
            List<OpportunitiesByNameForDisplay> OpportunitiesByNameForDisplay = new List<OpportunitiesByNameForDisplay>();
            SqlParameter[] sqlParams;
            List<object> real = new List<object>();
            try
            {
                var rswnumber = new SqlParameter("@AccountNumber", rswnum);

                sqlParams = new SqlParameter[1] {
                   rswnumber
                };

                string sql = "exec RSW_Reports_API.dbo.sp_CRMApp_GetAppointmentOpportunitiesFromZoomTesting " + rswnumber ;
                OpportunitiesByNameForDisplay = db.Database.SqlQuery<OpportunitiesByNameForDisplay>(sql,sqlParams).ToList();

                real = OpportunitiesByNameForDisplay.GroupBy(g => new List<Object> {
                    g.OpportunityName,
                    slide = new
                    {
                        g.Title,
                        g.Description,
                        g.Image,
                        g.opDate,
                        g.status,
                        g.SlideOrder,
                        g.displayorder
                    }
                });
            }
            catch (Exception ex){}
            return OpportunitiesByNameForDisplay;
        }

我在g.OpportunityName上获取了无效的初始化器声明符 有什么方法可以使这项工作更好?

1 个答案:

答案 0 :(得分:1)

只需从您发布的纯json并使用LINQ转换数据即可。我认为这与假设您想要json字符串之后的情况差不多。您要从方法中返回一个对象而不是序列化的字符串吗?。

将值放入关键位置的棘手部分可以通过ToDictionary()完成。

如果这不是您的理想解决方案,我希望它能提供一些意见。

var json = "[{\"displayorder\":2,\"oppItemID\":4,\"opportunityName\":\"Net 10\",\"title\":\"New Amazing Promotion\",\"description\":\"Amazing\",\"image\":\"imagelink\",\"opDate\":\"2018-08-09T22:00:00.937\",\"slideOrder\":0,\"status\":false,\"isCover\":false},{\"displayorder\":1,\"oppItemID\":2,\"opportunityName\":\"Simple Mobile\",\"title\":\"New promo\",\"description\":\"Amazing\",\"image\":\"imagelink\",\"opDate\":\"2018-08-09T22:00:00.937\",\"slideOrder\":0,\"status\":false,\"isCover\":false},{\"displayorder\":1,\"oppItemID\":3,\"opportunityName\":\"Simple Mobile\",\"title\":\"New Amazing Promotion\",\"description\":\"Amazing\",\"image\":\"imagelink\",\"opDate\":\"2018-08-10T22:00:00.937\",\"slideOrder\":0,\"status\":false,\"isCover\":false},{\"displayorder\":8,\"oppItemID\":5,\"opportunityName\":\"Verizon\",\"title\":\"New Amazing Promotion\",\"description\":\"Amazing\",\"image\":\"imagelink\",\"opDate\":\"2018-08-09T22:00:00.937\",\"slideOrder\":0,\"status\":false,\"isCover\":false},{\"displayorder\":8,\"oppItemID\":27,\"opportunityName\":\"Verizon\",\"title\":\"New Amazing\",\"description\":\"Amazing\",\"image\":\"imagelink\",\"opDate\":\"2018-08-22T22:00:00.937\",\"slideOrder\":0,\"status\":false,\"isCover\":false}]";
        var obj = JsonConvert.DeserializeObject<List<JSonClass>>(json);

        var transformed = obj 
                            .Select(  (value, index) => new {
                                            name = value.opportunityName,
                                            obj = new JSonClass2 {
                                                    displayorder = value.displayorder,
                                                    oppItemID = value.oppItemID,
                                                    title = value.title,
                                                    description = value.description,
                                                    image = value.image,
                                                    opDate = value.opDate,
                                                    slideOrder = value.slideOrder,
                                                    status = value.status,
                                                    isCover = value.isCover
                                                    }
                                            })
                .GroupBy(u=>u.name)  // groups by name
                .ToDictionary(       // use dictionary to move value from value position to key position 
                                wrap =>wrap.Key, 
                                wrap => wrap.Select( (v,i) => new { i, v.obj })
                .ToDictionary(w => w.i, w => w.obj )  
                );

        Console.WriteLine(JsonConvert.SerializeObject(transformed, Formatting.Indented));

控制台输出为:

{
  "Net 10": {
    "0": {
      "displayorder": 2,
      "oppItemID": 4,
      "title": "New Amazing Promotion",
      "description": "Amazing",
      "image": "imagelink",
      "opDate": "2018-08-09T22:00:00.937",
      "slideOrder": 0,
      "status": false,
      "isCover": false
    }
  },
  "Simple Mobile": {
    "0": {
      "displayorder": 1,
      "oppItemID": 2,
      "title": "New promo",
      "description": "Amazing",
      "image": "imagelink",
      "opDate": "2018-08-09T22:00:00.937",
      "slideOrder": 0,
      "status": false,
      "isCover": false
    },
    "1": {
      "displayorder": 1,
      "oppItemID": 3,
      "title": "New Amazing Promotion",
      "description": "Amazing",
      "image": "imagelink",
      "opDate": "2018-08-10T22:00:00.937",
      "slideOrder": 0,
      "status": false,
      "isCover": false
    }
  },
  "Verizon": {
    "0": {
      "displayorder": 8,
      "oppItemID": 5,
      "title": "New Amazing Promotion",
      "description": "Amazing",
      "image": "imagelink",
      "opDate": "2018-08-09T22:00:00.937",
      "slideOrder": 0,
      "status": false,
      "isCover": false
    },
    "1": {
      "displayorder": 8,
      "oppItemID": 27,
      "title": "New Amazing",
      "description": "Amazing",
      "image": "imagelink",
      "opDate": "2018-08-22T22:00:00.937",
      "slideOrder": 0,
      "status": false,
      "isCover": false
    }
  }
}