有如下一段字符
"skuMap":{
"灰色>L":{"canBookCount":905,"saleCount":91,"specId":"43b0e72e98731aed69e1f0cc7d64bf4d"},
"灰色>XL":{"canBookCount":929,"saleCount":70,"specId":"893746f5330dc3273d24aa1ac1a9a8b5"},
"灰色>XXL":{"canBookCount":942,"saleCount":57,"specId":"42d994cba0210528142a743d4069700f"},
"白色>L":{"canBookCount":862,"saleCount":136,"specId":"82114cbd2c10b5e97b01af1510807e2d"},
"白色>XL":{"canBookCount":881,"saleCount":118,"specId":"c45d8408137e34adf8e695250c42a2e9"},
"白色>XXL":{"canBookCount":917,"saleCount":82,"specId":"df78564262818d6eb0c428a37ab4a251"},
"蓝色>L":{"canBookCount":962,"saleCount":35,"specId":"e959b6ab7c355e403a3312c75bd3d5b4"},
"蓝色>XL":{"canBookCount":973,"saleCount":26,"specId":"27402e07efd89afa50733afa94cd6976"},
"蓝色>XXL":{"canBookCount":985,"saleCount":14,"specId":"358b6c3b52bf711ac8ecfe7513a4f3ad"}
},
要求存入一个集合中,按如下结构存入list<T>集合
感谢网友的参于与顶力相助,但希望能亲自测试下,因为本人测试发现这个JSON结构有点奇怪
颜色 尺码 库存 出售量 编号
----------------------------------------
灰色 L 905 91 43b0e72e98731aed69e1f0cc7d64bf4d
灰色 XL 929 70 893746f5330dc3273d24aa1ac1a9a8b5
{"skuMap":[ {"灰色":"L","canBookCount":905,"saleCount":91,"specId":"43b0e72e98731aed69e1f0cc7d64bf4d"}, {"灰色":"XL","canBookCount":929,"saleCount":70,"specId":"893746f5330dc3273d24aa1ac1a9a8b5"}, {"灰色":"XXL","canBookCount":942,"saleCount":57,"specId":"42d994cba0210528142a743d4069700f"}, {"白色":"L","canBookCount":862,"saleCount":136,"specId":"82114cbd2c10b5e97b01af1510807e2d"}, {"白色":"XL","canBookCount":881,"saleCount":118,"specId":"c45d8408137e34adf8e695250c42a2e9"}, {"白色":"XXL","canBookCount":917,"saleCount":82,"specId":"df78564262818d6eb0c428a37ab4a251"}, {"蓝色":"L","canBookCount":962,"saleCount":35,"specId":"e959b6ab7c355e403a3312c75bd3d5b4"}, {"蓝色":"XL","canBookCount":973,"saleCount":26,"specId":"27402e07efd89afa50733afa94cd6976"}, {"蓝色":"XXL","canBookCount":985,"saleCount":14,"specId":"358b6c3b52bf711ac8ecfe7513a4f3ad"} ] }
我需求的是转换成LIST对像,不是改变数据源对橡的结构
json没有所谓List<T>结构吧?只能是转换成数组以后再ToList()一下吧?你在怎么转的?用的json.net?
你提供的数据源就是数组,你把得到的这些数据进行ToList就可以了。
/// <summary> /// Json转换成实体类,返回对象 /// </summary> /// <typeparam name="T">反序列化类型</typeparam> /// <param name="jsonString">反序列化字符串</param> /// <returns>反序列化后的值</returns> public static T JsonToModel<T>(string jsonString) { using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString))) { try { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); T returnOjbect = (T)serializer.ReadObject(ms); return returnOjbect; } catch (Exception ex) { throw ex; } finally { ms.Close(); ms.Dispose(); } } } /// <summary> /// Json转换成List集合,返回对象List /// </summary> /// <typeparam name="T">反序列化类型</typeparam> /// <param name="jsonString">反序列化字符串</param> /// <returns>反序列化后的值</returns> public static List<T> JsonToList<T>(string jsonString) { return JsonToModel<List<T>>(jsonString); }
var obj = (JObject)(JObject.Parse(json)[
"skuMap"
]);
var result = from k
in
obj.Properties()
let names = k.Name.Split(
new
string
[] {
">"
}, StringSplitOptions.None)
let v = (JObject)k.Value
select
new
{
颜色 = names[0],
尺码 = names[1],
库存 = (
int
)v[
"canBookCount"
],
出售量 = (
int
)v[
"saleCount"
],
编号 = (
string
)v[
"specId"
]
};
var r = result.ToList();