【问题标题】:JSON RPC Serializing Object with SPECIFIC naming具有特定命名的 JSON RPC 序列化对象
【发布时间】: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+"\"]}" 这行得通..但是编写代码的方式非常低俗..

标签: c# json json-rpc json.net


【解决方案1】:

序列化程序对您的类所做的事情似乎很简单。

通常 JSON-RPC 服务将要求信封中的 params 值为 JSON Array(用于索引参数)或 Object(用于命名参数)。

你能不能改变你的班级,使 ParamsArrayString

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    相关资源
    最近更新 更多