0

If I cant split the json data in datarow using deserializeobject, is there any other way to do this?I have a set of data's in doc as "json", how can i split?

DataTable dt = new DataTable();            
foreach(BsonDocument doc in query)
{
    DataColumn dc = new DataColumn();
    if (doc !="null")
    {                    
    DataRow dr =(DataRow) Newtonsoft.Json.JsonConvert.DeserializeObject(doc.ToString(), (typeof(DataRow)));                    
     }

}

suga
  • 43
  • 1
  • 8
  • Can you please provide us input and what is expected – Prasad Telkikar Jun 20 '18 at 13:10
  • for example in doc value are like: {id:123,name:{firstname:abc, middlename:def, lastname:ijk},dob:100,industry:{industry1,industry2,industry3}}. i need to split and save it as a row. as i directly retrived data from db, it needs to be shown in a table/grid – suga Jun 20 '18 at 13:14

1 Answers1

0

I would deserialize the data in to a List of your object and once complete then create a function to iterate through the list and 'print' to the datarows. I have added a link below that shows how this is done. However there are multiple answers in the post, see which one you like and go from there.

stackoverflow answer

Nick B
  • 89
  • 6
  • var collection = mongoDatabase.GetCollection("ABC"); var query = collection.AsQueryable().Take(10).ToList(); DataSet data = Newtonsoft.Json.JsonConvert.DeserializeObject(query.ToString()); – suga Jun 21 '18 at 06:31
  • no.. sonConvert.DeserializeObject is not working for me. i dont know y.. then i tried with jqrid,. still foreach it written value but while adding to table it throws error.. it is because i have n number of data's in it. but i tried to execute first 10.. eventhough it is not working.. i will share my lastest code with the error.. – suga Jun 22 '18 at 07:35
  • DataSet data = JsonConvert.DeserializeObject(json); This can be a good method, this way you can follow it through with debug and ensure that you're getting the values you're looking for. Make sure that your json (doc) variable actually has valid data in it. – Nick B Jun 22 '18 at 13:24
  • It shows error like "CS0117 'JsonConvert' does not contain a definition for 'DeserializeObject' WebApplication"... I have used MongoDB.Bson.IO and also i used Newton.Json it shows like "Unexpected character encountered while parsing value: S. Path '', line 0, position 0. " for the dataset data =Newtonsoft.Json.JsonConvert.DeserializeObject(query.ToString()); where, query = collection.AsQueryable().Take(10).ToList(); – suga Jun 25 '18 at 10:16
  • also when i try to directly bind get to gridview it shows"Unable to cast object of type 'MongoDB.Bson.BsonDocument' to type 'MongoDB.Bson.BsonBoolean'. " – suga Jun 25 '18 at 10:35
  • I did have a similar issue with casting on my Android development, the data object I was trying to populate had a type that was unsupported and would not allow me to deserialize. If you're unable to cast then this could be part of the problem. You may want to comment out everything apart from one property(String) and test with that. Also make sure that the class is identical on both web service and application side. – Nick B Jun 25 '18 at 14:41
  • I am unsure if you can send me the code in a stackoverflow message and I can actually have a look at it. May be easier than multiple commenting on here? – Nick B Jun 25 '18 at 14:42