parse JSON file and insert data into SQL server
First I'd restructure the JSON so it made more sense. You said a "person" can have multiple "people", so structure it that way. A "person" has three attributes (i.e. i_date, i_location, and i_summary) and a collection of people.
{
"person":{
"i_date":"2014-03-20",
"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 .NET classes that represent the structure.
public class Person2
{
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<Person2> 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.
Console.WriteLine( root.person.i_date );
Console.WriteLine( root.person.i_location );
Console.WriteLine( root.person.i_summary );
foreach(var p in root.person.people)
{
Console.WriteLine( p.first_name );
Console.WriteLine( p.last_name );
}
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.
store json data in database using C#
The correct way to deserialize this content:
{
"Vikas": 1,
"Pravin": 2,
"Akshay": 5,
"Vijay": 3,
"Prasad": 4
}
Is to deserialize it into a Dictionary<string, int>
.
Here's a LINQPad example:
void Main()
{
const string json = @"{
""Vikas"": 1,
""Pravin"": 2,
""Akshay"": 5,
""Vijay"": 3,
""Prasad"": 4
}";
Dictionary<string, int> empsAndNumbers =
JsonConvert.DeserializeObject<Dictionary<string, int>>(json);
empsAndNumbers.Dump();
}
which outputs:
Convert JSON to DataTable
Deserialize your jsonstring to some class
List<User> UserList = JsonConvert.DeserializeObject<List<User>>(jsonString);
Write following extension method to your project
using System.ComponentModel;
public static DataTable ToDataTable<T>(this IList<T> data)
{
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
for(int i = 0 ; i < props.Count ; i++)
{
PropertyDescriptor prop = props[i];
table.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach (T item in data)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
return table;
}
Call extension method like
UserList.ToDataTable<User>();
c# - How to store json data into MySQL
Here the solution:
var persons = JsonConvert.DeserializeObject<List<Member>>(response);
now you can loop through items like below.
foreach(Member person in persons )
{
// add to db your each person
}
and here how you can insert to db :
for ADO.Net : https://msdn.microsoft.com/en-us/library/ms233812.aspx
for EntityFramework : https://msdn.microsoft.com/en-us/library/ee712907(v=vs.113).aspx
How to convert json format string to json in c#
This is an invalid JSON format, moreover it does not have surrounding array brackets []
. Ideally you should fix this at source.
You could do a simple replace
MyLabels = JsonConvert.DeserializeObject<List<Label>>(labelnames = "[" + labelnames.Replace("'", "\"") + "]")
However this may throw an error if any of the values also contain a '
.
Therefore, you could create a custom JsonTextReader
using (var sw = new StringReader("[" + labelnames + "]"))
using (var reader = new MyJsonTextReader(sw))
{
JsonSerializer ser = new JsonSerializer();
MyLabels = ser.Deserialize<List<Label>>(reader);
}
class MyJsonTextReader : JsonTextReader
{
public override char QuoteChar { get; protected set; } = '\'';
public MyJsonTextReader(TextReader r) : base(r){}
}
class Label
{
public string LabelName;
public bool IsHeader;
}
dotnetfiddle
Related Topics
How to Save/Download Pdf Embedded in Web Page Without a Pdf Filename
How to Check If User Input Is from Barcode Scanner or Keyboard
Copy Files With Authentication in C#
Angular Post Request Received in Net Core API as Null
Append a Lists Contents to Another List C#
Setting Connection String With Username and Password in Asp.Core MVC
Add Spaces Before Capital Letters
Generate Xml for <Govtalkmessage Xmlns="Http://Www.Govtalk.Gov.Uk/Cm/Envelope">
How to Get Cookies Info Inside of a Cookiecontainer (All of Them, Not for a Specific Domain)
Formatting Date in Linq-To-Entities Query Causes Exception
How to Reference Assemblies Using Visual Studio Code
Truncate Two Decimal Places Without Rounding
Convert Byte Array to Wav File
How to Ignore Convert to Nullable in Lambda Expressions
Encode a Filestream to Base64 With C#
How to Get the Xml Soap Request of an Wcf Web Service Request