将C#对象序列化为JSON数据字符串 [英] Serialize c# object to JSON datastring

查看:113
本文介绍了将C#对象序列化为JSON数据字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用json格式字符串的wcf webservice.我的代码是

[ServiceContract(Name ="StudentInformation")]
公共接口IService1
{

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

字符串GetStudentDetails(字符串Userid,字符串密码,字符串EntityId,字符串RequestAction,字符串RequestFilter);
}


//使用以下示例中所示的数据协定将复合类型添加到服务操作中.
[DataContract(Name ="StudentInfo")]
公共课程StudentList
{

[DataMember(Order = 1,Name ="StudentId")]
私有字符串idStudent;
公共字符串P_idstudent
{
获取
{
返回idStudent;
}
设置
{
idStudent =值;
}
}

[DataMember(Order = 2,Name ="Student Type")]
私有字符串StudentType;
公共字符串P_Studenttype
{
获取
{
返回StudentType;
}
设置
{
StudentType =值;
}
}

}


公共类Service1:IService1
{
公共字符串GetStudentDetails(字符串Userid,字符串密码,字符串EntityId,字符串RequestAction,字符串RequestFilter)
{
SqlConnection conn =新的SqlConnection(ConfigurationManager.ConnectionStrings ["DbConnection"].ToString());

列出< studentlist> lStudentList =新列表< studentlist>();
字符串sql =从学生中选择*";
SqlCommand命令=新的SqlCommand(sql,conn);
SqlDataAdapter da =新的SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
StudentList SList = new StudentList();
for(int i = 0; i< dt.Rows.Count; i ++)
{

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

}
DataContractJsonSerializer序列化器=新的DataContractJsonSerializer(lStudentList.GetType());
MemoryStream memoryStream =新的MemoryStream();
serializer.WriteObject(memoryStream,lStudentList);

字符串json = Encoding.Default.GetString(memoryStream.ToArray());
返回json;

}
}


我正在获取json格式的结果.

[{"StudentId":"2","Student Type":"1"},{"StudentId":"2","Student Type":"1"}]



但我需要像
这样的结果
{"studentInfo":[{"StudentId":"2","Student Type":"1"},{"StudentId":"2","Student Type":"1"}]}

我该怎么办才能使输出内容变为"studentInfo":

Hi,

I am working with wcf webservice with json format string. my code is

[ServiceContract(Name = "StudentInformation")]
public interface IService1
{

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

string GetStudentDetails(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 = "StudentInfo")]
public class StudentList
{

[DataMember(Order = 1, Name = "StudentId")]
private string idStudent;
public string P_idstudent
{
get
{
return idStudent;
}
set
{
idStudent = value;
}
}

[DataMember(Order = 2, Name = "Student Type")]
private string StudentType;
public string P_Studenttype
{
get
{
return StudentType;
}
set
{
StudentType = value;
}
}

}


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

List<studentlist> lStudentList = new List<studentlist>();
string sql = "select * from student";
SqlCommand command = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
StudentList SList = new StudentList();
for (int i = 0; i < dt.Rows.Count; i++)
{

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

}
DataContractJsonSerializer serializer = new DataContractJsonSerializer(lStudentList.GetType());
MemoryStream memoryStream = new MemoryStream();
serializer.WriteObject(memoryStream, lStudentList);

string json = Encoding.Default.GetString(memoryStream.ToArray());
return json;

}
}


I am getting the json formatted result.

[{"StudentId":"2","Student Type":"1"},{"StudentId":"2","Student Type":"1"}]



but I need the results like

{"studentInfo" : [{"StudentId":"2","Student Type":"1"},{"StudentId":"2","Student Type":"1"}]}

what can i do to get the output concated "studentInfo" :

推荐答案

DataContractJsonSerializer有一些错误.尝试其他JSON序列化程序,例如 FastJSON [ Json.NET [ ^ ].
DataContractJsonSerializer has some bugs. Try other JSON serializers, like FastJSON[^] or Json.NET[^].


这篇关于将C#对象序列化为JSON数据字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆