Can someone please provide example How to Insert JSON file in SQL tables using C#.
-
1Please read How to Ask and explain very explicitly what you mean by "insert JSON". JSON is just a string. Do you want to save it as a string? What is your question then?CodeCaster– CodeCaster2017-02-23 07:27:32 +00:00Commented Feb 23, 2017 at 7:27
-
I want to store JSON file data into SQL table using C#. by reading File and converting data in tabular format.user2741746– user27417462017-02-23 07:39:47 +00:00Commented Feb 23, 2017 at 7:39
Add a comment
|
1 Answer
Say you have the following JSON file:
{
"person":{
"i_date":"2017-02-23",
"i_location":"test",
"i_summary":"test test",
"people":[
{
"first_name":"first name test1",
"last_name":"last name test1"
},
{
"first_name":"first name test2",
"last_name":"last name test2"
},
{
"first_name": "first name test3",
"last_name":"last name test3"
}
]
}
}
Now you can declare some classes that represent the structure:
public class PersonalPerson
{
public string first_name { get; set; }
public string last_name { get; set; }
}
public class Person
{
public string i_date { get; set; }
public string i_location { get; set; }
public string i_summary { get; set; }
public List<PersonalPerson> people { get; set; }
}
public class RootObject
{
public Person person { get; set; }
}
Finally, use JsonConvert.DeserializeObject to get a set of object instances.
var root = JsonConvert.DeserializeObject<RootObject>( json );
You can now iterate over the "people" attached to the "person" and do stuff with it. At this point you can use either ADO.NET or Entity Framework to transfer the values from the objects into either SQL Parameters (ADO.NET) or EF classes to persist it into the database.
I hope that provides you the information you've needed
1 Comment
Dvir
Anotehr option is to follow the description here:stackoverflow.com/questions/7641004/…