Click here to Skip to main content
15,895,823 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I am working with wcf webservice which returns Json formatted output string. my output string is

[{"RecordTotal":{"_RecordCount":"1"},"StudentInfo":{"idStudent":"2","Associated_entity":"1","StudentType":"1"}}]

But i want the output like

{"RecordTotal":[{"_RecordCount":"1"}],
"StudentInfo":[{"idStudent":"2","Associated_entity":"1","StudentType":"1"}]}

The root node and the brackets should be in the above format. what shall i do?

My code is
C#
[ServiceContract]
 public interface IService1
 {

     [OperationContract]

     [WebInvoke(Method = "POST", UriTemplate = "/GetStudentDetailsCheck", RequestFormat = WebMessageFormat.Json,
     ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
     string GetStudentDetailsCheck(string Userid, string Password, string EntityId, string RequestAction, string RequestFilter);
 }


 // Use a data contract as illustrated in the sample below to add composite types to service operations.
 [DataContract(Name = "RecordCount")]
 public class RecordCount
 {
     [DataMember(Order = 1)]
     private string _RecordCount = string.Empty;

     public string RCount { get { return _RecordCount; } set { _RecordCount = value; } }

 }
 [DataContract(Name = "StudentInfo")]
 public class StudentList
 {

     [DataMember(Order = 2)]
     private string idStudent;
     public string P_idstudent
     {
         get
         {
             return idStudent;
         }
         set
         {
             idStudent = value;
         }
     }
     [DataMember(Order = 3)]
     private string Associated_entity;
     public string P_Associated_entity
     {
         get
         {
             return Associated_entity;
         }
         set
         {
             Associated_entity = value;
         }
     }
     [DataMember(Order = 4)]
     private string StudentType;
     public string P_Studenttype
     {
         get
         {
             return StudentType;
         }
         set
         {
             StudentType = value;
         }
     }




 }

 public class Results
 {
     [DataMember(Order = 1)]
     private RecordCount objRecordList = null;
     public RecordCount RecordTotal { get { return objRecordList; } set { objRecordList = value; } }
     [DataMember(Order = 2)]
     private StudentList objStudentList = null;
     public StudentList StudentInfo { get { return objStudentList; } set { objStudentList = value; } }

 }


public string GetStudentDetailsCheck(string Userid, string Password, string EntityId, string RequestAction, string RequestFilter)
     {
         SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnection"].ToString());

         List<StudentList> lStudentList = new List<StudentList>();
         List<RecordCount> lRecordCount = new List<RecordCount>();
         List<Results> lResultList = new List<Results>();
         string sql = "select S.*,Em.Org_id from student S inner join entity_master Em on S.associated_entity=Em.Identity_master and Em.org_id like '%" + RequestFilter + "%'";
         SqlCommand command = new SqlCommand(sql, conn);
         SqlDataAdapter da = new SqlDataAdapter(command);
         DataTable dt = new DataTable();
         da.Fill(dt);
         StudentList SList = new StudentList();
         RecordCount Rcounts = new RecordCount();
         Rcounts.RCount = (dt.Rows.Count - 1).ToString();
         lRecordCount.Add(Rcounts);
         string RequestFilters = RequestFilter;
         for (int i = 0; i < dt.Rows.Count; i++)
         {

             SList.P_idstudent = dt.Rows[i]["idstudent"].ToString();
             SList.P_Associated_entity = dt.Rows[i]["associated_entity"].ToString();
             SList.P_Studenttype = dt.Rows[i]["type"].ToString();

             lStudentList.Add(SList);

         }

         Results lstRes = new Results();
         lstRes.RecordTotal = Rcounts;
         lstRes.StudentInfo = SList;
         lResultList.Add(lstRes);

         System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new
         System.Runtime.Serialization.Json.DataContractJsonSerializer(lResultList.GetType());
         MemoryStream ms = new MemoryStream();
         serializer.WriteObject(ms, lResultList);
         string jsonString = Encoding.Default.GetString(ms.ToArray());
         ms.Close();
         return jsonString;
     }
Posted
Comments
loctrice 16-Sep-12 14:16pm    
I think the entire JSON is wrapped because you specified that in your response type along with the web invoke specification. The student info looks like you are json encoding an array, which would cause it to be wrapped (very bottom of your second code block window you are manually encoding an array/list)

Hope that helps.

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