【问题标题】:Get error while trying to parse into a JSON object尝试解析为 JSON 对象时出错
【发布时间】:2023-02-25 20:12:42
【问题描述】:

我想构建一个支付 API,但我总是收到以下错误:

{“解析值时遇到意外字符:e.Path '',第 0 行,位置 0。”}

代码如下所示:

var priceAmount = txtBoxTopUpBalance.Text;
            

var client = new RestClient("https://api.nowpayments.io/v1/invoice");
client.Timeout = -1;
var request1 = new RestRequest(Method.POST);
request1.AddHeader("x-api-key", apiKey);
request1.AddHeader("Content-Type", "application/json");
var body = @"{" + "\n" +
    @"  ""price_amount"":" + priceAmount + "\n" +
    @"  ""price_currency"": ""usd""," + "\n" +
    @"  ""order_id"": ""RGDBP-21314""," + "\n" +
    @"  ""order_description"": ""order #1""," + "\n" +
    @"  ""ipn_callback_url"": ""https://nowpayments.io""," + "\n" +
    @"  ""success_url"": ""https://nowpayments.io""," + "\n" +
    @"  ""cancel_url"": ""https://nowpayments.io""" + "\n" +
    @"}" + "\n" +
    @"" + "\n" +
    @"";
request1.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response1 = client.Execute(request1);

JObject responseObject = JObject.Parse(response1.Content); // <--The error occurs here
invoiceUrl = responseObject["invoice_url"].ToString();

问题是“正文”无法解析为 JSON 对象。

当我删除 priceAmount var 时它起作用了,但这是必要的。

我还是个初学者,你有什么想法吗?

【问题讨论】:

  • 您不是在大约一天前发布了基本相同的问题吗?请不要发布重复的问题,而是编辑现存的回应 cmets 要求更多信息的问题。

标签: c# json parsing serialization


【解决方案1】:

不要手动构建(序列化)JSON。很有可能会遇到语法错误,例如缺少引号、缺少左/右大括号等。

使用 JSON 库,例如系统.Text.Json或者牛顿软件.Json序列化为 JSON。

在示例 NOWPayments API Create Invoice documentation 中,您应该将 price_amount 作为数字类型传递。

using System.Text.Json;

var body = JsonSerializer.Serialize(new
{
    price_amount = decimal.Parse(priceAmount),
    price_currency = "usd",
    order_id = "RGDBP-21314",
    order_description = "order #1",
    ipn_callback_url = "https://nowpayments.io",
    success_url = "https://nowpayments.io",
    cancel_url = "https://nowpayments.io"
});

或者

using Newtonsoft.Json;

var body = JsonConvert.SerializeObject(new
{
    price_amount = decimal.Parse(priceAmount),
    price_currency = "usd",
    order_id = "RGDBP-21314",
    order_description = "order #1",
    ipn_callback_url = "https://nowpayments.io",
    success_url = "https://nowpayments.io",
    cancel_url = "https://nowpayments.io"
});

【讨论】:

    【解决方案2】:

    你有错字,需要在 priceAmount 之后加上“,”,但恕我直言,最好使用这种语法

        var client = new RestClient("https://api.nowpayments.io/");
        RestRequest request = new RestRequest("v1/invoice",Method.Post);
        request.AddHeader("Accept", "application/json");
        request.RequestFormat = DataFormat.Json;
    
        var body = new
        {
            price_amount = decimal.Parse(priceAmount),
            price_currency = "usd",
            order_id = "RGDBP-21314",
            order_description = "order #1",
            ipn_callback_url = "https://nowpayments.io",
            success_url = "https://nowpayments.io",
            cancel_url = "https://nowpayments.io"
        };
        request.AddJsonBody(body);
    
        var response = client.Execute(request).Content;
    JObject responseObject = JObject.Parse(response);
    var invoiceUrl = responseObject["invoice_url"].ToString();
    

    【讨论】:

      猜你喜欢
      • 2021-09-20
      • 2017-08-17
      • 1970-01-01
      • 2021-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      相关资源
      最近更新 更多