Ich bekomme ein JSON-Ergebnis von einer Javascript-API-Abfrage (keine Probleme, gültige JSON) und ich möchte es in MongoDb einfügen.JSON-String in mongoDB C# -Treiber v2.4.3 einfügen
Meine json string:
{"data":[{"accessible_wheelchair":true,"address":"181 Washington St","attire":"casual","category_ids":[344],"category_labels":[["Social","Food and Dining","Ice Cream Parlors"]],"country":"us","cuisine":["Ice Cream","Frozen Yogurt","Bagels","Deli","Donuts"],"factual_id":"403b11e4-c383-4305-8ba1-96aa6339eaba","hours":{"sunday":[["11:00","22:00"]],"saturday":[["11:00","22:00"]],"tuesday":[["11:00","22:00"]],"friday":[["11:00","22:00"]],"thursday":[["11:00","22:00"]],"wednesday":[["11:00","22:00"]],"monday":[["11:00","22:00"]]},"hours_display":"Open Daily 11:00 AM-10:00 PM","latitude":42.707169,"locality":"Boxford","longitude":-71.066385,"meal_deliver":false,"name":"Benson Ice Cream","open_24hrs":false,"parking":true,"payment_cashonly":false,"postcode":"01921","price":1,"rating":4.5,"region":"MA","tel":"(978) 352-2911","website":"http://bensonsicecream.com/","wifi":false},{"accessible_wheelchair":true,"address":"256 Georgetown Rd","address_extended":"Unit 5","attire":"casual","category_ids":[363],"category_labels":[["Social","Food and Dining","Restaurants","Pizza"]],"country":"us","cuisine":["Pizza","Cafe","Sandwiches","Subs"],"factual_id":"05e95c81-1125-447b-a500-84e0d380540d","fax":"(314) 423-3377","hours":{"sunday":[["11:00","21:00"]],"saturday":[["11:00","22:00"]],"tuesday":[["11:00","21:00"]],"friday":[["11:00","22:00"]],"thursday":[["11:00","21:00"]],"wednesday":[["11:00","21:00"]],"monday":[["11:00","21:00"]]},"hours_display":"Mon-Thu 11:00 AM-9:00 PM; Fri-Sat 11:00 AM-10:00 PM; Sun 11:00 AM-9:00 PM","latitude":42.697431,"locality":"Boxford","longitude":-70.988191,"meal_cater":true,"meal_deliver":true,"meal_takeout":true,"name":"Boxford House of Pizza","open_24hrs":false,"parking":true,"payment_cashonly":false,"postcode":"01921","price":1,"rating":4.5,"region":"MA","tel":"(978) 887-2212","website":"http://www.bostonrestaurantgroup.com","wifi":false}],"included_rows":2,"total_row_count":2}
poste ich die json string (Arraystring) zu einem C# Controller mit Ajax.
$.ajax({
url: '/Place/CreateMany',
type: 'POST',
contentType: 'application/json;',
data: JSON.stringify(arrayString),
success: function (valid) {
if (valid) {
alert("success ");
} else {
alert("failure" + arrayString);
}
}
});
Der Controller Fehler mit einem vage 500 internen Serverfehler.
[HttpPost]
public ActionResult CreateMany(string arrayString)
{
JObject jObject = JObject.Parse(arrayString);
JToken placeObj = jObject["data"];
foreach (string data in placeObj)
{
//have a working model defined called PlaceModel
//works with insertOne
var document = BsonSerializer.Deserialize<PlaceModel>(data);
//I am using a working repo(InsertPlace) that uses MongoDB method "insertOne"
//I would like to use a new repo that uses "insertMany"
this._places.InsertPlace(document);
}
return RedirectToAction("List", _places.SelectAll());
}
nicht sicher, was von JSON-String Array von mehr als einer auf ein einzelnes Objekt meiner Modelltyp (PlaceModel)
Dies ist mein erster Beitrag zu gehen zu tun, also bitte gehen Sie einfach, aber ich bin offen für Vorschläge.
So etwas wie 'IList Dokument = BsonSerializer.Deserialize > (Daten); collection.InsertMany (Dokument); ' –
Veeram