【问题标题】:JSON is valid but not working when sent to XeroJSON 有效但在发送到 Xero 时不起作用
【发布时间】:2021-12-15 19:28:38
【问题描述】:

我有以下 JSON,它已通过 https://jsonlint.com/ 验证。但是,当我通过 Xero API 运行它时,它会抛出一个错误,如下所示。

{
  "Type": "ACCREC",
  "Status": "AUTHORISED",
  "DueDate": "2021-12-11T14:24:08Z",
  "InvoiceNumber": "PRO152125",
  "Contact": {
    "ContactID": "ddd-05f9-46dd-b4a6-2ccbb5deb330"
  },
  "LineItems": [
    "{\"Description\": \"test1\", \"Qty\": 0.30, \"UnitAmount\": 950.0, \"TaxType\": \"OUTPUT2\", \"AccountCode\": \"200\"},\n{\"Description\": \"test2\", \"Qty\": 0.30, \"UnitAmount\": 300.0, \"TaxType\": \"OUTPUT2\", \"AccountCode\": \"200\"}"
  ]
}

{
  "ErrorNumber": 14,
  "Type": "PostDataInvalidException",
  "Message": "JSON for post data was invalid,Error converting value \"{\"Description\": \"test1\", \"Qty\": 0.30, \"UnitAmount\": 950.0, \"TaxType\": \"OUTPUT2\", \"AccountCode\": \"200\"},\n{\"Description\": \"test2\", \"Qty\": 0.30, \"UnitAmount\": 300.0, \"TaxType\": \"OUTPUT2\", \"AccountCode\": \"200\"}\" to type 'Xero.API.Library.DataContracts.LineItem'. Path 'LineItems[0]', line 1, position 417."
}

谁能帮忙解释为什么会这样?

【问题讨论】:

  • 有什么理由在 LineItems 中使用 JSON 作为字符串?
  • 我认为 Zapier 在尝试发送数据时会自动添加双引号。不知道如何阻止这种情况发生。
  • JSON 和 javascript 对象不是一回事。现在你有两者的混合,这是行不通的。 Zapier 正在发送 JSON(假设它是生成 LineItems 内容的原因);在将其插入对象的其余部分之前,您需要 JSON.parse
  • 如果你使用 JS 尝试使用 RegExp 来清理 LineItems,类似这样的东西 string.replace(/[\n"\&\r\t\b\f]/g, '\\ $&');
  • 这里没有理由求助于正则表达式,而且您可能会在此过程中破坏一些东西。 JSON.parse() 就是为此目的而设计的。

标签: javascript json xero-api


【解决方案1】:

API 正在尝试将您的 json 反序列化为类似的类

class MyClass 
{ 
    ..properties

  List<LineItem> LineItems...
}

class LineItem
{
  ... properties inside of the string
}

你的 json 是有效的,但它是

class MyClass 
{ 
    ..properties

  List<string> LineItems...
}

Api 序列化程序无法将 List 转换为 List。这就是导致错误的原因

你可以这样修复json

  var jsonObject=GetFixedJsonObject(json);
  
  var fixedJson=jsonObject.ToString();


public JObject GetFixedJsonObject(string json)
{
    var jsonObject = JObject.Parse(json);
    
    var jsonLineItems = "[" + (string)jsonObject["LineItems"][0] + "]";
    
    jsonObject["LineItems"] = JArray.Parse(jsonLineItems);
    
    return jsonObject;
}

固定的json

{
  "Type": "ACCREC",
  "Status": "AUTHORISED",
  "DueDate": "2021-12-11T14:24:08Z",
  "InvoiceNumber": "PRO152125",
  "Contact": {
    "ContactID": "ddd-05f9-46dd-b4a6-2ccbb5deb330"
  },
  "LineItems": [
    {
      "Description": "test1",
      "Qty": 0.3,
      "UnitAmount": 950.0,
      "TaxType": "OUTPUT2",
      "AccountCode": "200"
    },
    {
      "Description": "test2",
      "Qty": 0.3,
      "UnitAmount": 300.0,
      "TaxType": "OUTPUT2",
      "AccountCode": "200"
    }
  ]
}

【讨论】:

    【解决方案2】:

    您的订单项周围有引号,这意味着它只是被视为一个大字符串。试试这个:

    {
        "Type": "ACCREC",
        "Status": "AUTHORISED",
        "DueDate": "2021-12-11T14:24:08Z",
        "InvoiceNumber": "PRO152125",
        "Contact": {
            "ContactID": "ddd-05f9-46dd-b4a6-2ccbb5deb330"
        },
        "LineItems": [{
    
            "Description": "test1",
            "Qty": 0.30,
            "UnitAmount": 950.0,
            "TaxType": "OUTPUT2",
            "AccountCode": "200"
        }, {
            "Description": "test2",
            "Qty": 0.30,
            "UnitAmount": 300.0,
            "TaxType": "OUTPUT2",
            "AccountCode": "200"
        }]
    }
    

    【讨论】:

    • 它是一个有效的 JSON 对象吗?
    • 不是了,不是了。
    • 我认为 Zapier 在尝试发送数据时会自动添加双引号。不知道如何阻止这种情况发生。
    • 已编辑有效性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 2012-09-30
    • 2010-11-26
    相关资源
    最近更新 更多