如何从WCF服务返回JSON结果

如何从WCF服务返回JSON结果

本文介绍了如何从WCF服务返回JSON结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了WCF服务,我需要返回JSON结果,但我已经返回了List< string>。请帮我解决这个问题以返回JSON结果。



// IService.cs

I have created WCF service, I need to return JSON result, But i have returned List<string>. Kindly help me to solve this problem to return JSON result.

//IService.cs

namespace GenerateService
{
    [ServiceContract]
    public interface IService1
    {
         [OperationContract]
         List<string[]> AutoGenerate(string Content, int count);
    }
}







// Service1.cs






//Service1.cs

public class Service1 : IService1
    {
        public string datatypeval;

        public List<string[]> AutoGenerate(string Content, int count)
        {
            DataSet alRes = new DataSet();
            //string FieldValue;
            List<string[]> Getdetails = new List<string[]>();

            string sval;
            int s = 1; string[] Columns = Content.Split(',').Select(sValue => sValue.Trim()).ToArray();
            foreach (var item in Columns)
            {
                datatypeval += "'" + item + "'" + ",";
            }
            string FieldNames1 = "SELECT SUBSTRING((SELECT ',' + CAST(FId AS VARCHAR) FROM tbl_fieldname where DataTypeName in(" + datatypeval.Remove(datatypeval.Length - 1) + ")FOR XML PATH('')), 2,10000) AS FID";
            alRes = DAL.DBL.returnDataset(FieldNames1);
            string FieldNames = alRes.Tables[0].Rows[0]["FID"].ToString();
            string[] FieldID = FieldNames.Split(',').Select(sValue => sValue.Trim()).ToArray();
            for (int e = 0; e < Columns.Count(); e++)
            {
                //FieldValue = "";
                string qryGetFieldNames = "SELECT TOP (" + count + ")TD1.FID,TD1.Value,TFN.DataTypeName FROM tbl_Data1 TD1 INNER JOIN tbl_FieldName TFN ON TD1.FID=TFN.FID WHERE TD1.FId in (" + FieldID[e] + ") ORDER BY NEWID()";
                alRes = DAL.DBL.returnDataset(qryGetFieldNames);

                for (int i = 0; i < alRes.Tables[0].Rows.Count; i++)
                {
                    // FieldValue += Columns[e] + "::" + alRes.Tables[0].Rows[i]["Value"].ToString() + "~";

                    if (FieldID[e] == alRes.Tables[0].Rows[i]["FID"].ToString())
                    {
                        string[] Checkvalue = new string[FieldID.Length];
                        if (e != 0)
                        {
                            Checkvalue = Getdetails[i];
                        }
                        Checkvalue[e] = alRes.Tables[0].Rows[i]["Value"].ToString();
                        if (e == 0)
                        {
                            Getdetails.Add(Checkvalue);
                        }
                        else
                        {
                            Getdetails[i] = Checkvalue;
                        }
                    }
                }
                s++;
            }
            return Getdetails;
        }
    }





我的尝试:





What I have tried:

public class Service1 : IService1
    {
        public string datatypeval;

        public List<string[]> AutoGenerate(string Content, int count)
        {
            DataSet alRes = new DataSet();
            //string FieldValue;
            List<string[]> Getdetails = new List<string[]>();

            string sval;
            int s = 1; string[] Columns = Content.Split(',').Select(sValue => sValue.Trim()).ToArray();
            foreach (var item in Columns)
            {
                datatypeval += "'" + item + "'" + ",";
            }
            string FieldNames1 = "SELECT SUBSTRING((SELECT ',' + CAST(FId AS VARCHAR) FROM tbl_fieldname where DataTypeName in(" + datatypeval.Remove(datatypeval.Length - 1) + ")FOR XML PATH('')), 2,10000) AS FID";
            alRes = DAL.DBL.returnDataset(FieldNames1);
            string FieldNames = alRes.Tables[0].Rows[0]["FID"].ToString();
            string[] FieldID = FieldNames.Split(',').Select(sValue => sValue.Trim()).ToArray();
            for (int e = 0; e < Columns.Count(); e++)
            {
                //FieldValue = "";
                string qryGetFieldNames = "SELECT TOP (" + count + ")TD1.FID,TD1.Value,TFN.DataTypeName FROM tbl_Data1 TD1 INNER JOIN tbl_FieldName TFN ON TD1.FID=TFN.FID WHERE TD1.FId in (" + FieldID[e] + ") ORDER BY NEWID()";
                alRes = DAL.DBL.returnDataset(qryGetFieldNames);

                for (int i = 0; i < alRes.Tables[0].Rows.Count; i++)
                {
                    // FieldValue += Columns[e] + "::" + alRes.Tables[0].Rows[i]["Value"].ToString() + "~";

                    if (FieldID[e] == alRes.Tables[0].Rows[i]["FID"].ToString())
                    {
                        string[] Checkvalue = new string[FieldID.Length];
                        if (e != 0)
                        {
                            Checkvalue = Getdetails[i];
                        }
                        Checkvalue[e] = alRes.Tables[0].Rows[i]["Value"].ToString();
                        if (e == 0)
                        {
                            Getdetails.Add(Checkvalue);
                        }
                        else
                        {
                            Getdetails[i] = Checkvalue;
                        }
                    }
                }
                s++;
            }
            return Getdetails;
        }
    }

推荐答案

[OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
List<string[]> AutoGenerate(string Content, int count);





您可以根据您的响应结构生成类,您应该相应地返回这些类。



如果您有任何疑问或疑虑,请告诉我。



You can generate classes according to your response structure and You should return that classes accordingly.

Let me know if you have any query or concern.


这篇关于如何从WCF服务返回JSON结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 22:37