You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# Newtonsoft调用Zoho API报lineItems字符超100错误如何解决

问题根因

这个报错和lineItem的内容长度没有任何关系,是两个基础配置错误导致的参数解析失败:

  • 第一,Zoho Books创建发票接口要求line_items字段为数组类型,即使只有1条发票明细,也必须放在数组中传递。你当前代码里把lineItems定义成了单个对象,序列化后结构不符合接口要求;同时C#模型里的属性名是小驼峰的lineItems,和接口要求的下划线命名line_items不匹配,接口无法正确识别明细字段。
  • 第二,你设置的请求Content-Type值application / json; charset = UTF - 8存在多余空格(斜杠、等号两侧都加了无意义空格),属于非法MIME格式,会导致Zoho服务器无法识别请求为JSON格式,转而按普通表单参数解析请求体,此时序列化后的JSON内容会被当成普通字符串做长度校验,自然触发“长度需小于100字符”的报错。
修正方案

按以下步骤调整代码即可:

  1. 修正数据模型定义
    给属性配置序列化别名匹配接口的下划线命名规则,同时将line_items改为数组类型,金额类字段建议用decimal避免精度问题,长数字字段用long避免int溢出:
public class SmsBodyInvoices
{
    [JsonProperty("customer_id")]
    public long customer_id { get; set; }

    [JsonProperty("reference_number")]
    public string reference_number { get; set; }

    [JsonProperty("date")]
    public string date { get; set; }

    [JsonProperty("payment_terms")]
    public int payment_terms { get; set; }

    [JsonProperty("due_date")]
    public string due_date { get; set; }

    [JsonProperty("discount")]
    public decimal discount { get; set; }

    [JsonProperty("exchange_rate")]
    public decimal exchange_rate { get; set; }

    [JsonProperty("line_items")]
    public List<LineItem> line_items { get; set; }

    [JsonProperty("allow_partial_payments")]
    public bool allow_partial_payments { get; set; }

    [JsonProperty("notes")]
    public string notes { get; set; }

    [JsonProperty("terms")]
    public string terms { get; set; }

    [JsonProperty("shipping_charge")]
    public decimal shipping_charge { get; set; }

    [JsonProperty("adjustment")]
    public decimal adjustment { get; set; }

    [JsonProperty("adjustment_description")]
    public string adjustment_description { get; set; }
}

public class LineItem
{
    [JsonProperty("tags")]
    public tags tags { get; set; }

    [JsonProperty("description")]
    public string description { get; set; }

    [JsonProperty("item_order")]
    public int item_order { get; set; }

    [JsonProperty("bcy_rate")]
    public decimal bcy_rate { get; set; }

    [JsonProperty("rate")]
    public decimal rate { get; set; }

    [JsonProperty("quantity")]
    public int quantity { get; set; }

    [JsonProperty("unit")]
    public string unit { get; set; }

    [JsonProperty("discount_amount")]
    public decimal discount_amount { get; set; }

    [JsonProperty("discount")]
    public decimal discount { get; set; }

    [JsonProperty("header_name")]
    public string header_name { get; set; }
}
  1. 修正请求初始化逻辑
    明细对象用List包装,修正Content-Type的格式错误,顺便修正代码里的笔误:
SmsBodyInvoices smsBodyInvoices = new SmsBodyInvoices
{
    customer_id = customerIDNew,
    reference_number = "Test reference number",
    date = "2013-11-17",
    payment_terms = 15,
    due_date = "2013-12-03",
    discount = 0,
    exchange_rate = 1,
    line_items = new List<LineItem>
    { 
        new LineItem
        {
            description = "test description",
            item_order = 1,
            bcy_rate = 1,
            rate = 120,
            quantity = 1,
            unit = "",
            discount_amount = 0,
            header_name = "test header"
        }
    },
    allow_partial_payments = true,
    notes = "test note",
    terms = "test terms",
    shipping_charge = 0,
    adjustment = 0,
    adjustment_description = "adjust desc",
};

var bodyNew = Newtonsoft.Json.JsonConvert.SerializeObject(smsBodyInvoices);
requestZohoInvoices.AddParameter("application/json; charset=UTF-8", bodyNew, ParameterType.RequestBody);
IRestResponse responseZohoInvoices = clientZohoInvoices.Execute(requestZohoInvoices);
Console.WriteLine(responseZohoInvoices.Content);

内容的提问来源于stack exchange,提问作者Donavan John Wallis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 12:51:18