接口返回的字符串肉眼看起来正常,也是标准json,反序列化时候报错,
字符串添加了UTF8-BOM头(windows记事本默认编码),
可以通过以下代码移除标头
//模拟json字符串对象
var jsonStr="{}";
byte[] buffer = Encoding.UTF8.GetBytes(jsonStr);
var sResult = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
byte[] bomBuffer = new byte[] { 0xef, 0xbb, 0xbf };
if (buffer[0] == bomBuffer[0] && buffer[1] == bomBuffer[1] && buffer[2] == bomBuffer[2])
{
int copyLength = buffer.Length - 3;
byte[] dataNew = new byte[copyLength];
Buffer.BlockCopy(buffer, 3, dataNew, 0, copyLength);
sResult = System.Text.Encoding.UTF8.GetString(dataNew);
}
[参考]
C#解析json报错Unexpected character encountered while parsing value: . Path ‘‘, line 0, position 0.