Problem
How to return statement insert into table values from json file ?
I work on newton soft library and I try to insert data to table master_table
but my problem how to make function return insert statement as bellow :
- insert into master_table(id,branch_id,name,address,phone) values (1,1,"bar","fleet street","555")
table ,keys,fields content flexible or dynamic .
my jsonfile D:\\1.json as below :
- {
- "master" : {
- "table": "master_table",
- "fields": {
- "name" : "bar",
- "address" : "fleet street",
- "phone" : "555"
- },
- "keys":{
- "id" : 1,
- "branch_id" : 1
- }
- }
- }
I already make get keys and fields but cannot make concatenate
insert statement as result .
How to concatenate insert statement that have keys + fields
as statement on first thread .
- public static ExpandoObject ToExpando(string json)
- {
- if (string.IsNullOrEmpty(json))
- return null;
- return (ExpandoObject)ToExpandoObject(JToken.Parse(json));
- }
-
-
- private static object ToExpandoObject(JToken token)
- {
-
- switch (token.Type)
- {
- case JTokenType.Object:
- var expando = new ExpandoObject();
- var expandoDic = (IDictionary<string, object>)expando;
- foreach (var prop in token.Children<JProperty>())
- expandoDic.Add(prop.Name, ToExpandoObject(prop.Value));
- return expando;
- case JTokenType.Array:
- return token.Select(ToExpandoObject).ToList();
-
- default:
- return ((JValue)token).Value;
- }
- }
- static void Main(string[] args)
- {
- string JsonData = File.ReadAllText("D:\\1.json");
- var ebj = SqlFactory.ToExpando (JsonData);
- var name = (ebj as dynamic).master.table;
- var fields = (ebj as dynamic).master.fields;
- foreach (dynamic i in fields)
- {
- string key = i.Key;
- object value = i.Value;
- }
- var keys = (ebj as dynamic).master.keys;
-
- }