C#将Json字符串反序列化成List对象类集合

(“应用程序”下的“目标框架”选为“.Net Framework 4”測試)

using Newtonsoft.Json;
using System.Web.Script.Serialization;
using System.Runtime.Serialization.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WebService_DAO.VO
{
    public class RequestOutBoundVO
    {
        /// <summary>
        /// 出库类型
        /// </summary>
        public string OutBoundType { get; set; }

        /// <summary>
        /// 托盘条码
        /// </summary>
        public string Barcode { get; set; }

        /// <summary>
        /// 库位
        /// </summary>
        public string Rack { get; set; }

        /// <summary>
        /// 新库位 
        /// </summary>
        public string NewRack { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WebService_DAO.VO
{
    public class ResultVO
    {
        /// <summary>
        /// 结果
        /// </summary>
        public string ResultType { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string Message { get; set; }
    }
}
       public string RequestOutBound(string paramsJson)
        {
            //传出实体列表转Json
            IList<ResultVO> listResult = new List<ResultVO>();
            string strResult = string.Empty;
            try
            {
                //传入Json转实体列表
                JavaScriptSerializer Serializer = new JavaScriptSerializer();
                List<RequestOutBoundVO> list = Serializer.Deserialize<List<RequestOutBoundVO>>(paramsJson);


                //将传入数据写入数据表
                string strSql = "insert into {0} values({1},{2},{3},{4})";  //插入表的全部值,不用列名
                foreach (RequestOutBoundVO vo in list)
                {
                    int intResult = CommDAO.ExecuteCmd(string.Format(strSql, tableName,vo.OutBoundType, vo.Barcode, vo.Rack, vo.NewRack));
                     
                    //数据库操作失败
                    if (intResult != 1)
                    {
                        string Msg = "数据插入出错.";
                        throw new Exception(Msg);
                    }
                }

                //传出实体列表
                listResult.Add(new ResultVO()
                {
                    ResultType = "OK",
                    Message = "出库成功!"
                });

                //OK:实体列表转Json
                strResult = JsonConvert.SerializeObject(listResult, Formatting.Indented);
                return strResult;
            }
            catch (Exception ex)
            {
                //传出实体列表
                listResult.Add(new ResultVO()
                {
                    ResultType = "NG",
                    Message = ex.ToString()
                });
                //NG:实体列表转Json
                strResult = JsonConvert.SerializeObject(listResult, Formatting.Indented);
                return strResult;
            }
        }

 

参考:
https://www.cnblogs.com/seanyan/p/14184995.html
https://blog.csdn.net/qingkaqingka/article/details/88812351
cnblogs.com/wzyexf/p/4856691.html
https://www.cnblogs.com/chrisghb8812/p/5590980.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值