C# 如何在C中使用Json.Net从文件中读取Json集合#
我有这个json文件:C# 如何在C中使用Json.Net从文件中读取Json集合#,c#,json,json.net,C#,Json,Json.net,我有这个json文件: { “transactions”:[ { “type”:”deposit”, “account_id”:123456789012345, “amount”:20000.0 }, { “type”:”deposit”, “account_id”:555456789012345, “amount”:20000.0 }
{
“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));
}
您的错误:
可能是因为使用了卷曲引号而不是直接引号。用标准的直接引号替换它
编辑:
如果您在JSON解析方面遇到更多问题,我将这样解析JSON:
string jsonString = @"{
""transactions"": [
{
""type"": ""deposit"",
""account_id"": 123456789012345,
""amount"": 20000
},
{
""type"": ""deposit"",
""account_id"": 555456789012345,
""amount"": 20000
},
{
""type"": ""payment"",
""account_id"": 123456789012345,
""amount"": 20000
},
{
""type"": ""transfer"",
""from"": 555456789012345,
""to"": 123456789012345,
""amount"": 20000
}
]
}";
var recordObject = JObject.Parse(jsonString);
如果我想得到最后一笔交易
var lastRecord = JObject.Parse(jsonString)["transactions"].Last()
如果我想要存款记录
var deposits = from transactions in JObject.Parse(jsonString)["transactions"]
where transactions["type"].ToString().Equals("deposit")
select transactions;
您在哪里使用
var记录
?请告诉我更多关于它的信息。。。我不能理解除了“类型”:“存款”它应该是“类型”:“存款”。您应该将“
和”
的所有实例替换为“
”,非常感谢。但是还有一个错误!请参阅Update2,它似乎是一个错误的JSON格式错误。转到并将您的JSON粘贴到那里,看看它是否有效:)
var lastRecord = JObject.Parse(jsonString)["transactions"].Last()
var deposits = from transactions in JObject.Parse(jsonString)["transactions"]
where transactions["type"].ToString().Equals("deposit")
select transactions;