将JSON中的数据读取到类中并存储到数据库sqlserver中

时间:2018-10-16 08:32:32

标签: c# json sql-server json.net

class Program {
static void Main(string[] args)
{
    //This is your input json string
    var inputJson = @"{
                  'datamapItems': [
                               {
                                  'paramName': 'VE8321C',
                                  'datamapKey': {
                                               'module': 1,
                                               'id': 1391
                                  },
                                  'min': '0',
                                  'max': '40',
                                  'default': 222,
                                  'rateHz': 0,
                                  'timeoutMs': 0,
                                  'dataType': 'uint16'
                                }
                             ]
                       }";

        clsJSON result = JsonConvert.DeserializeObject<clsJSON>(inputJson); 
    }
}
public class clsJSON
{
      public Datamap datamap;
}
public class Datamap
{
    [JsonExtensionData]
    public Dictionary<string, JToken> DatamapFields = new Dictionary<string, JToken>();
    public List<DatamapItem> DatamapItems;
}
public class DatamapItem
{
    [JsonExtensionData]
    public Dictionary<string, JToken> DatamapItems = new Dictionary<string, JToken>();
    public DatamapKey DatamapKey;
}
public class DatamapKey
{
    [JsonExtensionData]
    public Dictionary<string, JToken> DatamapKeys = new     Dictionary<string, JToken>();
}

我已经使用给定的类结构将Json数据读入类,如上所示。 现在,我想将读取的数据插入数据库。 clsJSON结果= JsonConvert.DeserializeObject(inputJson); 该行从json中读取所有数据。

要插入的表格矩阵:

db_table_name   json_table_name db_column_name  json_column_name
datamapItems    tblECISOCode    code_name   paramName
datamapKey          tblECISOCode    producer_module module
datamapItems    tblECISOCode    ec_iso_code_id  id
datamapItems    tblECISOCode    minimum         min
datamapItems    tblECISOCode    maximum         max
datamapItems    tblECISOCode    NULL            rateHz

下面是json模板

{
    "datamap": {
        "__comment__dataMapElements__": "",
        "version_cfg_data": "0",
        "version_json_syntax": "0",
        "fnd_chains_input_file": "Configurator",
        "datamapItems": [{
            "paramName": "tblECISOCode@code_name",
            "datamapKey": {
                "module": "tblECISOCode@producer_module,
                "id": "tblECISOCode@ec_iso_code_id"
            },
            "min": "tblECISOCode@minimum",
            "max": "tblECISOCode@maximum",
            "default": 0,
            "rateHz": "tblECISOCode@broadcast_frequency_rate_id->FK(tblDataType@data_type)",
            "dataType": "tblECISOCode@data_type_id->FK(tblDataType@data_type)",
            "internalUnit": "tblECISOCode@internal_unit_id->FK(tblInternalUnit@internal_unit)",
            "displayUnit": "tblECISOCode@display_unit_id->FK(tblInternalUnit@internal_unit)",
            "GuiName": "tblECISOCode@description",
            "application": "tblECISOCode@application_id->FK(tblApplicationEnable@application)",
            "accessRole": "access_role_id->FK(tblRole@role)"
        },      
        "__comment__json_format__": "If tblECISOCode@is_cylinder_code is true, then:"       
        {
            "paramName": "tblECISOCode@code_name+1", cat("tblECISOCode@code_name","1"),     test1
            "datamapKey": {
                "module": tblECISOCode@producer_module[1],
                "id": tblECISOCode@ec_iso_code_id
            },
            "min": tblECISOCode@minimum,
            "max": tblECISOCode@maximum,
            "default": 0,
            "rateHz": tblECISOCode@broadcast_frequency_rate,
            "dataType": "tblECISOCode@data_type_id->FK(tblDataType@data_type)",
            "internalUnit": "tblECISOCode@internal_unit_id->FK(tblInternalUnit@internal_unit)",
            "displayUnit": "tblECISOCode@display_unit_id->FK(tblInternalUnit@internal_unit)",
            "GuiName": "tblECISOCode@description",
            "application": "tblECISOCode@application_id->FK(tblApplicationEnable@application)",
            "accessRole": "access_role_id->FK(tblRole@role)"
        },
        {
            "paramName": "tblECISOCode@code_name+2",test2
            "datamapKey": {
                "module": tblECISOCode@producer_module[2],
                "id": tblECISOCode@ec_iso_code_id
            },
            "min": tblECISOCode@minimum,
            "max": tblECISOCode@maximum,
            "default": 0,
            "rateHz": tblECISOCode@broadcast_frequency_rate,
            "dataType": "tblECISOCode@data_type_id->FK(tblDataType@data_type)",
            "internalUnit": "tblECISOCode@internal_unit_id->FK(tblInternalUnit@internal_unit)",
            "displayUnit": "tblECISOCode@display_unit_id->FK(tblInternalUnit@internal_unit)",
            "GuiName": "tblECISOCode@description",
            "application": "tblECISOCode@application_id->FK(tblApplicationEnable@application)",
            "accessRole": "access_role_id->FK(tblRole@role)"
        },
        ......
        {
            "paramName": "tblECISOCode@code_name+12",test12
            "datamapKey": {
                "module": tblECISOCode@producer_module[12],
                "id": tblECISOCode@ec_iso_code_id
            },
            "min": tblECISOCode@minimum,
            "max": tblECISOCode@maximum,
            "default": 0,
            "rateHz": tblECISOCode@broadcast_frequency_rate,
            "dataType": "tblECISOCode@data_type_id->FK(tblDataType@data_type@data_type)",
            "internalUnit": "tblECISOCode@internal_unit_id->FK(tblInternalUnit@internal_unit)",
            "displayUnit": "tblECISOCode@display_unit_id->FK(tblInternalUnit@internal_unit)",
            "GuiName": "tblECISOCode@description",
            "application": "tblECISOCode@application_id->FK(tblApplicationEnable@application)",
            "accessRole": "access_role_id->FK(tblRole@role)"
        }
}

上面的json提供了详细的模板。

0 个答案:

没有答案