需要封装的数据
![](https://images2015.cnblogs.com/blog/549046/201604/549046-20160425102939642-1293866598.jpg)
将这些数据,分组出来,OLGoodsID相同的为一组,然后每个组的OLSKUID,放在一个字段里,变成
[{"OLGoodID":"test06261551","OLSKUID":"51112,155111,155113"},{"OLGoodID":"test06261618","OLSKUID":"062611,062612,062613"}]
代码实现:
![](https://images2015.cnblogs.com/blog/549046/201604/549046-20160425102950142-628194925.png)
[{"OLGoodID":"test06261551","OLSKUID":"51112,155111,155113"},{"OLGoodID":"test06261618","OLSKUID":"062611,062612,062613"}]
string OLGoodsID = "";
JArray arrGoods = new JArray();
for (int j = 0; j < dtResult.Rows.Count; j++)
{
JObject CurrObjGoods = new JObject();
if (OLGoodsID != dtResult.Rows[j]["OLGoodsID"].ToString())
{
OLGoodsID = dtResult.Rows[j]["OLGoodsID"].ToString();
CurrObjGoods["OLGoodsID"] = dtResult.Rows[j]["OLGoodsID"].ToString();
CurrObjGoods["OLSKUID"]= dtResult.Rows[j]["OLSKUID"].ToString();
arrGoods.Add(CurrObjGoods);
}
else {
arrGoods[arrGoods.Count-1]["OLSKUID"] = arrGoods[arrGoods.Count-1]["OLSKUID"].ToString()+"," + dtResult.Rows[j]["OLSKUID"].ToString();
}
}