0
javascriptのapiクエリ(問題なし、有効なjson)からjson結果が得られます。mongoDbに挿入したいと思います。mongoDBのC#ドライバv2.4.3にjsonの文字列を挿入します。
私のJSON文字列:
{"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}
私は、AjaxとC#のコントローラにJSON文字列(arrayString)を投稿してください。
$.ajax({
url: '/Place/CreateMany',
type: 'POST',
contentType: 'application/json;',
data: JSON.stringify(arrayString),
success: function (valid) {
if (valid) {
alert("success ");
} else {
alert("failure" + arrayString);
}
}
});
コントローラが曖昧な500内部サーバーエラーでエラー出力します。私のモデルタイプの単一オブジェクト(PlaceModel)に複数のJSON文字列配列から行くために何をすべきか
[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());
}
わからない
これはそう簡単に行くが、私してください、私の最初の投稿です提案に開放されています。
にjsonStringと 'のIList文書のようなものをjsonString = BsonSerializer.Deserialize >(データ); collection.InsertMany(document); ' –
Veeram