2017-03-25 8 views
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()); 
    } 

わからない

これはそう簡単に行くが、私してください、私の最初の投稿です提案に開放されています。

+0

にjsonStringと 'のIList 文書のようなものをjsonString = BsonSerializer.Deserialize >(データ); collection.InsertMany(document); ' – Veeram

答えて

0

ヴェラムが正しい。 JSONではなくBSONのシリアル化を使用する必要がありました。 Ajaxのポストで

[HttpPost] 
    public void CreateMany(string jsonString) 
    { 
     //sets up mongo connection, DB and collection 
     var Client = new MongoClient(); 
     var DB = Client.GetDatabase("PlacesDb"); 
     var collection = DB.GetCollection<PlaceModel>("PlaceCollection"); 

     if (jsonString != null) 
     { 
      IList<PlaceModel> documents = BsonSerializer.Deserialize<IList<PlaceModel>>(jsonString); 
      collection.InsertMany(documents); 
     } 
    } 

、私はデータ型を追加する必要がありました: 'JSON' と配列jsonStringとしての私のJSON文字列を渡します:[]カッコ

self.saveToMongoDB = function (jsonString) { 
    $.ajax({ 
     url: '/Place/CreateMany', 
     type: 'POST', 
     contentType: 'application/json; charset=utf-8', 
     dataType: 'json', 
     data: JSON.stringify({ jsonString: jsonString }), 
     success: function (valid) { 
      if (valid) { 
       alert("success " + jsonString); 
      } else { 
       alert("failure" + jsonString); 
      } 
     } 
    }); 
}; 
関連する問題