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
[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);
}
[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;
}