【发布时间】:2014-08-12 09:36:01
【问题描述】:
我有这个 json 文件:
{
“transactions”:[
{
“type”:”deposit”,
“account_id”:123456789012345,
“amount”:20000.0
},
{
“type”:”deposit”,
“account_id”:555456789012345,
“amount”:20000.0
},
{
“type”:”payment”,
“account_id”:123456789012345,
“amount”:20000.0
},
{
“type”:”transfer”,
“from”:555456789012345,
“to”:123456789012345,
“amount”:20000.0
}
]
}
我想用 JSON.net 读取这个文件。
我试过这段代码,但它有一些未处理的错误:
var records = JsonConvert.DeserializeObject<List<Type>>(File.ReadAllText(FileAddress));
using (StreamReader SrFile = File.OpenText(FileAddress))
{
JsonSerializer Serializer = new JsonSerializer();
JsonAccount newJsonAccount = (JsonAccount)Serializer.Deserialize(SrFile, typeof(JsonAccount));
}
错误是:
Newtonsoft.Json.dll 中出现“Newtonsoft.Json.JsonReaderException”类型的未处理异常
附加信息:解析时遇到意外字符 价值:�.路径 '',第 0 行,第 0 位置
现在它有这个错误:
附加信息:将值“事务”转换为类型“ImportAndExport.MainForm+JsonAccount”时出错。路径 '',第 1 行,位置 14。
更新 2:
现在错误是:
An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll
Additional information: Invalid character after parsing property name. Expected ':' but got: t. Path '', line 2, position 7.
【问题讨论】:
-
你在哪里使用
var records?