【问题标题】:How to Read Json Collection From File Using Json.Net in C#如何在 C# 中使用 Json.Net 从文件中读取 Json 集合
【发布时间】: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

标签: c# json json.net


【解决方案1】:

你的错误:

Additional information: Unexpected character encountered while parsing value: �. Path '', line 0, position 0.

可能是由于您使用弯引号而不是直引号引起的。将其替换为标准直引号。

编辑:

如果您在解析 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;

【讨论】:

  • 请告诉我更多关于它的信息......我无法理解
  • 应该是“type”:”deposit”,而不是"type":"deposit"。您应该将所有 实例替换为 "
  • 非常感谢。但是还有一个错误!见更新2
  • 这似乎是一个糟糕的 JSON 格式错误。转到jsonlint.com 并将您的 JSON 粘贴到那里,看看它是否验证:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-01
  • 1970-01-01
  • 2016-02-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多