输入json
{
"ErrorMessage":"Transaction has been authorized successfully",
"ControlId":1000.00,
"Authorizations":[
{
"RMATranUUID":"1c1a88f7-d6cf-4ae8-87d3-ba06e9d9fe36",
"Payments":[
{
"PaymentNumber":"1",
"TotalPaymentsNumber":24,
"AmountDue":1000.0,
"AmountPaid":0.00
}
],
"Term":24,
"OTBReleaseAmount":null
},
{
"RMATranUUID":"b012ba9c-2dbd-4961-8959-ec0afbafbe13",
"OTBReleaseAmount":null
}
]
}
解析后输出json
JObject jsonPacket = JObject.Parse(inputString);
//行无变化,从1000.00更改为1000.0
{
"ErrorMessage":"Transaction has been authorized successfully",
"ControlId":1000.0,
"Authorizations":[
{
"RMATranUUID":"1c1a88f7-d6cf-4ae8-87d3-ba06e9d9fe36",
"Payments":[
{
"PaymentNumber":"1",
"TotalPaymentsNumber":24,
"AmountDue":1000.0,
"AmountPaid":0.00
}
],
"Term":24,
"OTBReleaseAmount":null
},
{
"RMATranUUID":"b012ba9c-2dbd-4961-8959-ec0afbafbe13",
"OTBReleaseAmount":null
}
]
}
答案 0 :(得分:0)
原因是解析双精度的JObject在后台执行如下操作:
double data;
double.TryParse("1214.00", NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out data);
由于数据的类型为double,double是浮点数,因此您没有获得期望的值。
您可以创建一个POCO并将其解析为它,或者可以执行以下操作:
string json = "{ \"ErrorMessage\":\"Transaction has been authorized successfully\", \"ControlId\":1000.00, \"Authorizations\":[ { \"RMATranUUID\":\"1c1a88f7-d6cf-4ae8-87d3-ba06e9d9fe36\", \"Payments\":[ { \"PaymentNumber\":\"1\", \"TotalPaymentsNumber\":24, \"AmountDue\":1000.0, \"AmountPaid\":0.00 } ], \"Term\":24, \"OTBReleaseAmount\":null }, { \"RMATranUUID\":\"b012ba9c-2dbd-4961-8959-ec0afbafbe13\", \"OTBReleaseAmount\":null } ]}";
var jsonReader = new JsonTextReader(new StringReader(json));
jsonReader.FloatParseHandling = FloatParseHandling.Decimal;
var jObject = JObject.Load(jsonReader);
Console.WriteLine(jObject["ControlId"].Value<decimal>()); // 1000.00
Console.WriteLine(jObject); // prints your actual json