<分区>
我是 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; }
}