gpt4 book ai didi

C# 将文档从 mongodb 读取到 JSON 中并尝试获取值

转载 作者:可可西里 更新时间:2023-11-01 09:44:12 26 4
gpt4 key购买 nike

<分区>

我是 JSON 的新手,所以这可能是一个简单的问题。我有下面的代码(下面还有一个 json 对象)。

我只想能够读取 JSON 并将其中的项目放入变量中以使其可读。

我收到错误,这是由于返回 JSON 对象中的文本 ObjectID(""):解析值时遇到意外字符:O.路径“_id”,第 1 行,位置 10。

从 MONGO 返回的 JSON 对象:

{
"_id": ObjectId("543c2d426b6b5ef78b62fc41"),
"LoanStatusAddedEvent": {
"@From": "Lakewood",
"@MessageTimeDateStamp": "2013-08-12T05:03:23.035-04:00",
"@MessageID": "29055040",
"LoanNumber": "5300494930",
"CurrentIndicator": "true",
"StatusChangeDate": "2013-08-11T20:01:01.000",
"StatusValue": "42",
"StatusType": "Active"
}
}

代码:

        // connect to the mongo server and the Mongos DB
string connectionString = "connectionstringhere";


MongoServer mongo = MongoServer.Create(connectionString);
mongo.Connect();

MongoDatabase database = mongo.GetDatabase("Mangos");


//Builds new Collection (looking at JsonImports on server)
MongoCollection<LoanStatus> collection2 = database.GetCollection<LoanStatus>("JsonImports");


var collection = database.GetCollection<BsonDocument>("JsonImports");


var query = new QueryDocument(); //("LoanNumber", "5300494930");


foreach (BsonDocument item in collection.Find(query))
{
string json = item.ToJson();

Console.WriteLine(item);


// code here to below http://james.newtonking.com/json/help/index.html?topic=html/QueryingLINQtoJSON.htm
JObject rss = JObject.Parse(json);

string rssLoanNumber = (string)rss["LoanStatusAddedEvent"]["LoanNumber"];

Console.WriteLine(rssLoanNumber);



}// end for each loop


Console.WriteLine();
Console.Read();


public class LoanStatus
{
public string From { get; set; }

public string MessageTimeDateStamp { get; set; }

public string MessageID { get; set; }

public string LoanNumber { get; set; }

public string CurrentIndicator { get; set; }

public string StatusChangeDate { get; set; }

public string StatusValue { get; set; }

public string StatusType { get; set; }

//public string id { get; set; }



public void PrintDetailsToScreen()
{
Console.WriteLine(String.Format("{0}{1}{2}",
this.LoanNumber, this.StatusChangeDate, this.StatusValue));
}

}

public class RootObject
{
public LoanStatus LoanStatus { get; set; }
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com