如何将JSON字符串转换为SQL Server表?

时间:2015-02-04 05:25:10

标签: c# json

我想创建一个生成这种JSON字符串的表:

{
    "imagename": "Picture1",
    "date": "03.09.2014",
    "customer": {
    "customernumber": "8",
    "name": "any customer name"
  }
}

1 个答案:

答案 0 :(得分:1)

按原样创建表,但不包括字段客户,如下所示:

CREATE TABLE [dbo].[aTable](
    [ImageName] [nchar](10) NULL,
    [Date] [nchar](10) NULL,
    [CustomerNumber] [nchar](10) NULL,
    [Name] [nchar](10) NULL
) ON [PRIMARY]

从表中选择数据并创建动态对象以分配数据(硬编码值为例):

var dyna = new {imageName = "Picture1", date = "03.02.2014", customer = new {customerNumber = "8", name = "any cust name"}};

然后使用JSON序列化程序序列化dyna对象。