[英]Serializing .NET object to JSON
I tried the following code to serialize a .NET object to JSON, but i keep getting blank text. 我尝试使用以下代码将.NET对象序列化为JSON,但我不断收到空白文本。 What am I doing wrong? 我究竟做错了什么?
[DataContract]
public class JsonObject2
{
[DataMember(Name = "field1")]
string field1 { get; set; }
[DataMember(Name = "field2")]
string field2 { get; set; }
[DataMember(Name = "field3")]
string[] test = { "heshan", "perera" };
}
The object, I attempt to serialize and display the resulting JSON string in a message box, but all i get is blank. 对象,我尝试在消息框中序列化并显示生成的JSON字符串,但我得到的只是空白。
MemoryStream s = new MemoryStream();
DataContractJsonSerializer dcjs2 = new DataContractJsonSerializer((typeof(JsonObject2)));
JsonObject2 obj2 = new JsonObject2();
dcjs2.WriteObject(s, obj2);
StreamReader r = new StreamReader(s);
String x = r.ReadToEnd();
MessageBox.Show(x);
Try adding: 尝试添加:
s.Position = 0;
just before you create the StreamReader.
就在创建StreamReader.
之前StreamReader.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.