[英]Newtonsoft.Json.JsonSerializationException: 'Unexpected token when deserializing object: Comment with Dynamic Object
I am trying to deserialize the JSON to a object using Newtonsoft.Json.我正在尝试使用 Newtonsoft.Json 将 JSON 反序列化为 object。 Our input json request has commented line.
我们输入的 json 请求已注释行。
{
"paymentMethodToken": "bb3vph6",
"paymentMethodTypes": "ACH",
"transactionAmount": "1090.9",
"transactionType": "Charge",
"transactionID": "3532464245",
"merchantAccountID": "643765867",
"insuredName": "First Last",
"firstName": "First",
"lastName": "Last",
// "sourceUserKey": "example@gmail.com"
"paymentMethodTokenType": "Durable",
"paymentType": "Recurring",
"sourceTransactionId": "OrderACH300"
}
we wanted to ignore/skip the comments while deserializing.我们想在反序列化时忽略/跳过评论。 I believe Json.Net does this by default.
我相信 Json.Net 默认会这样做。
But I am getting error as但我收到错误
Newtonsoft.Json.JsonSerializationException: 'Unexpected token when deserializing object: Comment. Newtonsoft.Json.JsonSerializationException:'反序列化 object 时出现意外令牌:评论。 Path 'lastName', line 11
路径“姓氏”,第 11 行
Below is the code I am using for deserialization下面是我用于反序列化的代码
string valueFromBody;
using (var streamReader = new StreamReader(bindingContext.HttpContext.Request.Body))
{
valueFromBody = streamReader.ReadToEnd();
}
//Deserilaize body content to model instance
var modelType = bindingContext.ModelMetadata.UnderlyingOrModelType;
var modelInstance = JsonConvert.DeserializeObject(valueFromBody, modelType)
Newtonsoft.Json Version: 12.0.3 Newtonsoft.Json 版本:12.0.3
Model Class Model Class
[Serializable]
public class GatewayTransaction : DynamicObject
{
private CaseInSensitiveDictionary<string, string> customFields = new CaseInSensitiveDictionary<string, string>();
public string PaymentMethodToken { get; set; }
public PaymentMethodTypes PaymentMethodTypes { get; set; }
public string InvoiceId { get; set; }
public decimal TransactionAmount { get; set; }
public string ChannelID { get; set; }
public string TransactionType { get; set; }
public string TransactionID { get; set; }
public string MerchantAccountID { get; set; }
public string InsuredName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string SourceUserKey { get; set; }
public string PaymentMethodTokenType { get; set; }
public PaymentType PaymentType { get; set; }
public List<TransactionInfo> PolicyInfo { get; set; }
public string SourceTransactionId { get; set; }
[JsonIgnore]
public CaseInSensitiveDictionary<string, string> CustomFields
{
get { return this.customFields; }
}
public override bool TryGetMember(GetMemberBinder binder, out object value)
{
string stringValue;
var isFound = this.customFields.TryGetValue(binder.Name, out stringValue);
value = stringValue;
return isFound;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
if (value is string)
{
this.customFields[binder.Name] = (string)value;
return true;
}
return false;
}
}
Any help would be appreciated.任何帮助,将不胜感激。
A different approach is preprocess your JSON string.另一种方法是预处理您的 JSON 字符串。 Strip out all the comments before de-serializing.
在反序列化之前去掉所有注释。 You could do something like
你可以做类似的事情
static string StripComments(string code)
{
var re = @"(@(?:""[^""]*"")+|""(?:[^""\n\\]+|\\.)*""|'(?:[^'\n\\]+|\\.)*')|//.*|/\*(?s:.*?)\*/";
return Regex.Replace(code, re, "$1");
}
Using the above function, You can strip out single and multiline comments from JSON.使用上面的 function,您可以从 JSON 中去除单行和多行注释。 Then do de-serializing.
然后进行反序列化。
Attributed to: Remove all comment (single-/multi-line) & blank lines from source file归因于: 从源文件中删除所有注释(单行/多行)和空白行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.