JObject SelectToken 解析路径索引器时出现意外字符
JObject SelectToken Unexpected character while parsing path indexer
我需要解析 API json 响应以提取特定值。直到今晚,这种方法对我来说一直很有效,没有问题。尝试执行此代码时:
JObject oR = JObject.Parse(response2.Content);
if (oR.SelectToken("[response][0][assignment][current][channelId]") != null)
{
ChannelId = oR.SelectToken("[response][0][assignment][current][channelId]").ToString();
}
我收到此错误:
Unexpected character while parsing path indexer: r
这是我正在尝试解析的 api 的完整 json 响应:
{
"response": [
{
"id": 268633,
"parentOrderId": 0,
"orderTypeCode": "SO",
"reference": "#351569",
"version": 3,
"state": {
"tax": "READY"
},
"orderStatus": {
"orderStatusId": 41,
"name": "Ready to Ship"
},
"tax": {
"errors": []
},
"orderPaymentStatus": "PAID",
"stockStatusCode": "SOA",
"allocationStatusCode": "AAA",
"shippingStatusCode": "SNS",
"placedOn": "2021-10-07T18:53:54.000-05:00",
"createdOn": "2021-10-07T18:54:12.000-05:00",
"updatedOn": "2021-10-07T18:57:43.000-05:00",
"createdById": 4,
"priceListId": 3,
"priceModeCode": "EXC",
"delivery": {
"deliveryDate": "2021-10-06T19:00:00.000-05:00",
"shippingMethodId": 9
},
"invoices": [
{
"invoiceReference": "",
"taxDate": "2021-10-07T00:00:00.000-05:00",
"dueDate": "2021-10-06T19:00:00.000-05:00"
}
],
"currency": {
"accountingCurrencyCode": "USD",
"orderCurrencyCode": "USD",
"exchangeRate": "1.000000",
"fixedExchangeRate": true
},
"totalValue": {
"net": "14.10",
"taxAmount": "1.16",
"baseNet": "14.10",
"baseTaxAmount": "1.16",
"baseTotal": "15.26",
"total": "15.26"
},
"assignment": {
"current": {
"staffOwnerContactId": 0,
"projectId": 0,
"channelId": 3,
"leadSourceId": 0,
"teamId": 0
}
},
"parties": {
"customer": {
"contactId": 748240,
"addressFullName": "John Doe",
"companyName": "",
"addressLine1": "225 N First ST",
"addressLine2": "",
"addressLine3": "My Town",
"addressLine4": "Texas",
"postalCode": "76082-2583",
"country": "United States",
"telephone": "+1900MIXALOT",
"mobileTelephone": "",
"fax": "",
"email": "john.doe@yahoo.com",
"countryId": 223,
"countryIsoCode": "US",
"countryIsoCode3": "USA"
},
"delivery": {
"addressFullName": "John Doe",
"companyName": "",
"addressLine1": "225 N First ST",
"addressLine2": "",
"addressLine3": "My Town",
"addressLine4": "Texas",
"postalCode": "76082-2583",
"country": "United States",
"telephone": "+1900MIXALOT",
"mobileTelephone": "",
"fax": "",
"email": "john.doe@yahoo.com",
"countryId": 223,
"countryIsoCode": "US",
"countryIsoCode3": "USA"
},
"billing": {
"contactId": 748240,
"addressFullName": "John Doe",
"companyName": "",
"addressLine1": "225 N First ST",
"addressLine2": "",
"addressLine3": "My Town",
"addressLine4": "Texas",
"postalCode": "76082-2583",
"country": "United States",
"telephone": "+1900MIXALOT",
"mobileTelephone": "",
"fax": "",
"email": "john.doe@yahoo.com",
"countryId": 223,
"countryIsoCode": "US",
"countryIsoCode3": "USA"
}
},
"orderRows": {
"600995": {
"orderRowSequence": "10",
"productId": 30727,
"productName": "Mens Wrangler Sport Western Plaid Snap - M",
"productSku": "395802",
"quantity": {
"magnitude": "1.0000"
},
"itemCost": {
"currencyCode": "USD",
"value": "11.2500"
},
"productPrice": {
"currencyCode": "USD",
"value": "24.9900"
},
"discountPercentage": "0.00",
"rowValue": {
"taxRate": "9.5000",
"taxCode": "T",
"taxCalculator": "manual",
"rowNet": {
"currencyCode": "USD",
"value": "4.9900"
},
"rowTax": {
"currencyCode": "USD",
"value": "0.4000"
},
"taxClassId": 2
},
"productOptions": {
"Top Size": "M",
"Color": "Multi"
},
"nominalCode": "4000",
"composition": {
"bundleParent": false,
"bundleChild": false,
"parentOrderRowId": 0
},
"externalRef": "10197546795147",
"clonedFromId": 0
},
"600997": {
"orderRowSequence": "30",
"productId": 1001,
"productName": "Shipping: UPS SurePost",
"productSku": "",
"quantity": {
"magnitude": "1.0000"
},
"itemCost": {
"currencyCode": "USD",
"value": "0.0000"
},
"productPrice": {
"currencyCode": "USD",
"value": "9.1100"
},
"discountPercentage": "0.00",
"rowValue": {
"taxRate": "9.5000",
"taxCode": "T",
"taxCalculator": "manual",
"rowNet": {
"currencyCode": "USD",
"value": "9.1100"
},
"rowTax": {
"currencyCode": "USD",
"value": "0.7600"
},
"taxClassId": 2
},
"nominalCode": "4030",
"composition": {
"bundleParent": false,
"bundleChild": false,
"parentOrderRowId": 0
},
"clonedFromId": 0
},
"600996": {
"orderRowSequence": "20",
"productId": 1000,
"productName": "Coupon: PDJA046209",
"productSku": "",
"quantity": {
"magnitude": "1.0000"
},
"itemCost": {
"currencyCode": "USD",
"value": "0.0000"
},
"productPrice": {
"currencyCode": "USD",
"value": "0.0000"
},
"discountPercentage": "0.00",
"rowValue": {
"taxRate": "0.0000",
"taxCode": "-",
"taxCalculator": "brightpearl",
"rowNet": {
"currencyCode": "USD",
"value": "0.0000"
},
"rowTax": {
"currencyCode": "USD",
"value": "0.00"
},
"taxClassId": 1
},
"productOptions": {
"Top Size": "S",
"Color": "Green"
},
"nominalCode": "4020",
"composition": {
"bundleParent": false,
"bundleChild": false,
"parentOrderRowId": 0
},
"clonedFromId": 0
}
},
"warehouseId": 3,
"acknowledged": 0,
"costPriceListId": 1,
"historicalOrder": false,
"externalRef": "3912489140363",
"installedIntegrationInstanceId": 484,
"orderWeighting": 100
}
]
}
不幸的是,我无法控制从网络服务 API 返回的 json。我能找到的最好的结果是 json 键之一有问题,但我似乎无法弄清楚如何修复它或解释它。
在此先感谢您的帮助。
正确的 JSONPath 语法是:
response[0].assignment.current.channelId
...或使用 enquoted 属性 名称:
['response'][0]['assignment']['current']['channelId']
我个人更喜欢前一种语法,因为它更清晰易读。
此外,您的程序也很低效,因为您正在评估 JSONPath 两次。而是使用 C# 的 is var
运算符来存储结果:
JObject oR = JObject.Parse(response2.Content);
if (oR.SelectToken("response[0].assignment.current.channelId") is JToken channelIdToken)
{
this.ChannelId = channelIdToken.ToString();
}
我需要解析 API json 响应以提取特定值。直到今晚,这种方法对我来说一直很有效,没有问题。尝试执行此代码时:
JObject oR = JObject.Parse(response2.Content);
if (oR.SelectToken("[response][0][assignment][current][channelId]") != null)
{
ChannelId = oR.SelectToken("[response][0][assignment][current][channelId]").ToString();
}
我收到此错误:
Unexpected character while parsing path indexer: r
这是我正在尝试解析的 api 的完整 json 响应:
{
"response": [
{
"id": 268633,
"parentOrderId": 0,
"orderTypeCode": "SO",
"reference": "#351569",
"version": 3,
"state": {
"tax": "READY"
},
"orderStatus": {
"orderStatusId": 41,
"name": "Ready to Ship"
},
"tax": {
"errors": []
},
"orderPaymentStatus": "PAID",
"stockStatusCode": "SOA",
"allocationStatusCode": "AAA",
"shippingStatusCode": "SNS",
"placedOn": "2021-10-07T18:53:54.000-05:00",
"createdOn": "2021-10-07T18:54:12.000-05:00",
"updatedOn": "2021-10-07T18:57:43.000-05:00",
"createdById": 4,
"priceListId": 3,
"priceModeCode": "EXC",
"delivery": {
"deliveryDate": "2021-10-06T19:00:00.000-05:00",
"shippingMethodId": 9
},
"invoices": [
{
"invoiceReference": "",
"taxDate": "2021-10-07T00:00:00.000-05:00",
"dueDate": "2021-10-06T19:00:00.000-05:00"
}
],
"currency": {
"accountingCurrencyCode": "USD",
"orderCurrencyCode": "USD",
"exchangeRate": "1.000000",
"fixedExchangeRate": true
},
"totalValue": {
"net": "14.10",
"taxAmount": "1.16",
"baseNet": "14.10",
"baseTaxAmount": "1.16",
"baseTotal": "15.26",
"total": "15.26"
},
"assignment": {
"current": {
"staffOwnerContactId": 0,
"projectId": 0,
"channelId": 3,
"leadSourceId": 0,
"teamId": 0
}
},
"parties": {
"customer": {
"contactId": 748240,
"addressFullName": "John Doe",
"companyName": "",
"addressLine1": "225 N First ST",
"addressLine2": "",
"addressLine3": "My Town",
"addressLine4": "Texas",
"postalCode": "76082-2583",
"country": "United States",
"telephone": "+1900MIXALOT",
"mobileTelephone": "",
"fax": "",
"email": "john.doe@yahoo.com",
"countryId": 223,
"countryIsoCode": "US",
"countryIsoCode3": "USA"
},
"delivery": {
"addressFullName": "John Doe",
"companyName": "",
"addressLine1": "225 N First ST",
"addressLine2": "",
"addressLine3": "My Town",
"addressLine4": "Texas",
"postalCode": "76082-2583",
"country": "United States",
"telephone": "+1900MIXALOT",
"mobileTelephone": "",
"fax": "",
"email": "john.doe@yahoo.com",
"countryId": 223,
"countryIsoCode": "US",
"countryIsoCode3": "USA"
},
"billing": {
"contactId": 748240,
"addressFullName": "John Doe",
"companyName": "",
"addressLine1": "225 N First ST",
"addressLine2": "",
"addressLine3": "My Town",
"addressLine4": "Texas",
"postalCode": "76082-2583",
"country": "United States",
"telephone": "+1900MIXALOT",
"mobileTelephone": "",
"fax": "",
"email": "john.doe@yahoo.com",
"countryId": 223,
"countryIsoCode": "US",
"countryIsoCode3": "USA"
}
},
"orderRows": {
"600995": {
"orderRowSequence": "10",
"productId": 30727,
"productName": "Mens Wrangler Sport Western Plaid Snap - M",
"productSku": "395802",
"quantity": {
"magnitude": "1.0000"
},
"itemCost": {
"currencyCode": "USD",
"value": "11.2500"
},
"productPrice": {
"currencyCode": "USD",
"value": "24.9900"
},
"discountPercentage": "0.00",
"rowValue": {
"taxRate": "9.5000",
"taxCode": "T",
"taxCalculator": "manual",
"rowNet": {
"currencyCode": "USD",
"value": "4.9900"
},
"rowTax": {
"currencyCode": "USD",
"value": "0.4000"
},
"taxClassId": 2
},
"productOptions": {
"Top Size": "M",
"Color": "Multi"
},
"nominalCode": "4000",
"composition": {
"bundleParent": false,
"bundleChild": false,
"parentOrderRowId": 0
},
"externalRef": "10197546795147",
"clonedFromId": 0
},
"600997": {
"orderRowSequence": "30",
"productId": 1001,
"productName": "Shipping: UPS SurePost",
"productSku": "",
"quantity": {
"magnitude": "1.0000"
},
"itemCost": {
"currencyCode": "USD",
"value": "0.0000"
},
"productPrice": {
"currencyCode": "USD",
"value": "9.1100"
},
"discountPercentage": "0.00",
"rowValue": {
"taxRate": "9.5000",
"taxCode": "T",
"taxCalculator": "manual",
"rowNet": {
"currencyCode": "USD",
"value": "9.1100"
},
"rowTax": {
"currencyCode": "USD",
"value": "0.7600"
},
"taxClassId": 2
},
"nominalCode": "4030",
"composition": {
"bundleParent": false,
"bundleChild": false,
"parentOrderRowId": 0
},
"clonedFromId": 0
},
"600996": {
"orderRowSequence": "20",
"productId": 1000,
"productName": "Coupon: PDJA046209",
"productSku": "",
"quantity": {
"magnitude": "1.0000"
},
"itemCost": {
"currencyCode": "USD",
"value": "0.0000"
},
"productPrice": {
"currencyCode": "USD",
"value": "0.0000"
},
"discountPercentage": "0.00",
"rowValue": {
"taxRate": "0.0000",
"taxCode": "-",
"taxCalculator": "brightpearl",
"rowNet": {
"currencyCode": "USD",
"value": "0.0000"
},
"rowTax": {
"currencyCode": "USD",
"value": "0.00"
},
"taxClassId": 1
},
"productOptions": {
"Top Size": "S",
"Color": "Green"
},
"nominalCode": "4020",
"composition": {
"bundleParent": false,
"bundleChild": false,
"parentOrderRowId": 0
},
"clonedFromId": 0
}
},
"warehouseId": 3,
"acknowledged": 0,
"costPriceListId": 1,
"historicalOrder": false,
"externalRef": "3912489140363",
"installedIntegrationInstanceId": 484,
"orderWeighting": 100
}
]
}
不幸的是,我无法控制从网络服务 API 返回的 json。我能找到的最好的结果是 json 键之一有问题,但我似乎无法弄清楚如何修复它或解释它。
在此先感谢您的帮助。
正确的 JSONPath 语法是:
response[0].assignment.current.channelId
...或使用 enquoted 属性 名称:
['response'][0]['assignment']['current']['channelId']
我个人更喜欢前一种语法,因为它更清晰易读。
此外,您的程序也很低效,因为您正在评估 JSONPath 两次。而是使用 C# 的 is var
运算符来存储结果:
JObject oR = JObject.Parse(response2.Content);
if (oR.SelectToken("response[0].assignment.current.channelId") is JToken channelIdToken)
{
this.ChannelId = channelIdToken.ToString();
}