服务返回JSON格式数据

以WCF服务为例

方法调用

string jsonresult = MyBaseManageProxy.SaveStorage(MySe, UserID);

Contract

 [OperationContract]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        string SaveStorage(StorageEntity se, string userId);

Service

  public string SaveStorage(StorageEntity se, string userId)
        {
            return JsonConvert.SerializeObject(MyBaseBLL.SaveStorage(se, userId));
        }
    }
}

BILL

  public FeedbackInfomation SaveStorage(StorageEntity se, string userId)
        {
            FeedbackInfomation fi = new FeedbackInfomation();
            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    if (se.StorageID == null || se.StorageID == "")
                    {
                        se = MyBaseDAL.InsertStorageEntity(se);
                      //  LogEntity le = this.GenerateLog("基础维护", "仓库维护", "新增", DateTime.Now, userId, "");
                       // le = MyBaseDAL.InsertLogEntity(le);
                        //LogDetailEntity lde = this.GenerateLogDetail(le.LogID, "Com_StorageRoom", se.StorageID, "");
                       // lde = MyBaseDAL.InsertLogDetailEntity(lde);


                    }
                    else
                    {
                        se = MyBaseDAL.UpdateStorageEntity(se);
                        //LogEntity le = this.GenerateLog("基础维护", "仓库维护", "修改", DateTime.Now, userId, "");
                        //le = MyBaseDAL.InsertLogEntity(le);
                        //LogDetailEntity lde = this.GenerateLogDetail(le.LogID, "Com_StorageRoom", se.StorageID, "");
                        //lde = MyBaseDAL.InsertLogDetailEntity(lde);
                    }
                    scope.Complete();
                }
                fi.ErrorStatus = STATUS_ADAPTER.SAVE_SUCCESS;
                fi.Result = se;
                fi.FeedbackMessage = Tips.SAVE_SUCCESS;
                return fi;
            }
            catch (Exception ex)
            {
                fi.ErrorStatus = STATUS_ADAPTER.SAVE_FAILED;
                fi.Result = "";
                fi.FeedbackMessage = Tips.SAVE_FAILED;
                return fi;
            }
        }

FeedbackInfomation

namespace CMST.Storage.Server.Common
{
    [DataContract]
    public class FeedbackInfomation
    {
        [DataMember]
        public object Result { get; set; }

        [DataMember]
        public STATUS_ADAPTER ErrorStatus { get; set; }

        [DataMember]
        public string FeedbackMessage { get; set; }
    }
}

解析JOSN为LIST

 FeedbackInfomation fi = JsonConvert.DeserializeObject(jsonresult);
            if(fi.ErrorStatus == STATUS_ADAPTER.QUERY_NORMAL)
            {
                MySes = JsonConvert.DeserializeObject>(fi.Result.ToString());
                this.View.SetDataSource(MySes);
            }
            else
            {
                this.View.ShowTips(fi.FeedbackMessage);
            }
  public void SetDataSource(List ses)
        {
            this.dgvStorageInfo.DataSource = new List();
            this.dgvStorageInfo.DataSource = ses;
        }

你可能感兴趣的:(服务返回JSON格式数据)