首页 新闻 会员 周边 捐助

求JSON格式转换成LIST格式

0
[待解决问题]

有如下一段字符
"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

土农民的主页 土农民 | 初学一级 | 园豆:19
提问于:2013-05-30 14:48
< >
分享
所有回答(6)
0
{"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"}
]
}
TiestoRay | 园豆:687 (小虾三级) | 2013-05-30 14:53

我需求的是转换成LIST对像,不是改变数据源对橡的结构

支持(0) 反对(0) 土农民 | 园豆:19 (初学一级) | 2013-05-30 14:57
0

json没有所谓List<T>结构吧?只能是转换成数组以后再ToList()一下吧?你在怎么转的?用的json.net?

顾晓北 | 园豆:10893 (专家六级) | 2013-05-30 15:18
0

你提供的数据源就是数组,你把得到的这些数据进行ToList就可以了。

坤坤 | 园豆:919 (小虾三级) | 2013-05-30 15:59
0
        /// <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);
        }
淘@淘 | 园豆:602 (小虾三级) | 2013-05-30 16:56
0
var obj = (JObject)(JObject.Parse(json)["skuMap"]);
var result = from k in obj.Properties()
                let names = k.Name.Split(new string[] { "&gt;" }, 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();
土农民 | 园豆:19 (初学一级) | 2014-03-27 22:18
0

用这个工具可以很轻松解决,http://www.ofmonkey.com/

独孤飞 | 园豆:283 (菜鸟二级) | 2017-01-16 16:15
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册