繁体   English   中英

反序列化由包含bson数据类型的mongodb生成的json

[英]Deserialize json generated by mongodb that contains bson datatypes

我收到了一些JSON数据文件 - 但是,每个对象都包含BSON数据类型; 最重要的是,它是一个非常大的tojson转储(数百万条记录)。

我试图反序列化数据,并按预期失败。

JSON文件包含以下内容:

"someKey" : NumberLong("1234567889"),

它还有ISODate ......

有没有办法用Json.net来处理这个问题? 似乎可能有一些设置让它使用自定义函数而不是特定键的内置解析器?

*更新为包括非常大的流(+ 100GB +文件)的流+文本阅读器的代码

using (StreamReader file = File.OpenText(@"\\largedump.txt"))
            using (JsonTextReader reader = new JsonTextReader(file))
            {
                reader.SupportMultipleContent = true;    
                var serializer = new JsonSerializer();
                while (reader.Read())
                {
                    if (reader.TokenType == JsonToken.StartObject)
                    {
                        Contacts c = serializer.Deserialize<Contacts>(reader);
                        Console.WriteLine(c.orgId);
                    }
                }
            }

你可以使用mongo驱动程序bson序列化器:

使用MongoDB.Bson.Serialization;

  var bjson = @"{
                        '_id' : ObjectId('57ac672e34780e59784d7d2a'),
                        'ActivePick' : null,
                        'EventCodeId' : null,
                        'Frame' : { '$binary' : 'AgY=', '$type' : '00' },
                        'FrameTimeStamp' : ISODate('2016-08-11T11:53:18.541Z'),
                        'ServerUserId' : 0,
                        'ServerUserName' : null,
                        'SesionId' : 0,
                        'TraderId' : null,
                        'TraderName' : null
                    }";

        var bsonDocument = BsonDocument.Parse(bjson);
        var myObj = BsonSerializer.Deserialize<FrameDocument>(bsonDocument);

来源于

编辑

我对给定的方法没有任何问题。 请参阅github解决方案,因为它正在序列化而没有问题。

            string line;
            using (TextReader file = File.OpenText("ImportDataFromBJsonFile\\a.json"))
            {
                while ((line = file.ReadLine()) != null)
                {
                    var bsonDocument = BsonDocument.Parse(line);
                    var myObj = BsonSerializer.Deserialize<Zxed>(bsonDocument);
                }
            }

来源sln项目

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM