Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

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;
        }
    }
Posted
Updated 26-Jul-19 0:03am

1 solution

Define response format like this in interface

[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.
 
Share this answer
 
Comments
vinodh muthusamy 4-May-17 5:26am    
I have created above code in interface,

what changes should i make in Services1.svc.cs for JSON result
Nirav Prabtani 4-May-17 5:32am    
Check your response first, Isn't it is json ?
vinodh muthusamy 4-May-17 6:59am    
How to check, Whether it is returning Json or not.

I think iam getting like XMl format
Nirav Prabtani 4-May-17 7:30am    
check your response in jsonlint website
vinodh muthusamy 5-May-17 0:16am    
It Returns as an XML format only

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900