【发布时间】:2012-04-26 12:18:26
【问题描述】:
我使用 NewtonSoft linq 2 json 将对象从类直接序列化为 json 字符串
我使用的类对象很简单:
public class OverviewQuery
{
public string id { get; set; }
public string method { get; set; }
public string Params { get; set; }
public OverviewQuery(string sid, string smethod, string sparam)
{
this.id = sid;
this.method = smethod;
this.Params = sparam;
}
}
如果我序列化这个,我会得到 Json 字符串:
"{\"id\":\"1\",\"method\":\"getStockItemDetails\",\"Params\":\"0000000002\"}"
我(通过 WebAPI)连接到的 Oracle 服务器要求我使用非常具体的命名, 这里应该是
"{\"id\":\"1\",\"method\":\"getStockItemDetails\",\"Params\":[\"0000000002\"]}"
NewtonSoft 有没有办法实现这种格式? 如果没有正确的格式,发送信息的唯一方法是对所有内容进行硬编码。
【问题讨论】:
-
我暂时通过在已格式化的查询之间添加一个数据绑定字符串来解决此问题,换句话说就是部分硬编码.. "{\"id\":\"1\",\"method \":\"getStockItemDetails\",\"Params\":[\""+idref+"\"]}" 这行得通..但是编写代码的方式非常低俗..