【发布时间】:2018-09-27 21:04:11
【问题描述】:
我正在尝试将对象列表序列化为 json,但在 json 中,所有数据成员都被“\”包围。 比如我的对象是:
public class ResObject
{
public double productPrice { get; set; }
public string productName { get; set; }
public double distance { get; set; }
public string fullAddress { get; set; }
public string fullStoreName { get; set; }
public string promotion { get; set; }
}
结果 json 为:
"[{\"productPrice\":4,\"productName\":\"Milk\",\"distance\":0,\"fullAddress\":\"UK\",\"fullStoreName\":\"LiaStore\",\"promotion\":null},...]"
我使用了这个代码:
string json = JsonConvert.SerializeObject(Products, Formatting.None);
或
var json = new JavaScriptSerializer().Serialize(Products);
或
//Create a stream to serialize the object to.
MemoryStream ms = new MemoryStream();
// Serializer the User object to the stream.
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<ResObject>));
ser.WriteObject(ms, Products);
byte[] json = ms.ToArray();
ms.Close();
return Encoding.UTF8.GetString(json, 0, json.Length);
但它没有帮助:(
(产品为List<ResObject> Products)
谢谢!
编辑:我修复了它,问题是我序列化了列表并返回了它,因为它是 WebAPI,这就是问题所在。解决方法是返回列表并删除序列化。
【问题讨论】:
-
反斜杠,必须是在Watch中调试模式查看值时。对吗?
标签: c# json jsonserializer