如何拆分/解析 JSON 数据并将其保存到 SQL 服务器中?

我有一个 MVC 控制器来接收从 WinForms 应用程序发送的 JSON 数据。如何拆分 JSON 字符串并将其存储到 SQL 服务器中?


这是我接收和反序列化 json 对象的控制器:


Stream req = Request.InputStream;

req.Seek(0,System.IO.SeekOrigin.Begin);

string json = new StreamReader(req).ReadToEnd();

hl7View hl = null;

hl = JsonConvert.DeserializeObject<hl7View>(json);

//hl do have a string data like this

//"{\"data\":[{\"LOC\":\"TEST\",\"COMPANY\":\"BST\",\"MSG_ID\":\"20160701123200-112\",\"NS\":\"\",\"ROOM\":\"\",\"BED\":\"\",\"SID\":\"25444444\",\"OBS_SEQ\":\"1\",\"OBS_TIME\":\"20160701164801\",\"SEQ\":\"1\",\"ITEM\":\"8867-4\",\"ITEMLAB\":\"Heart Rate\",\"RESULT\":\"80\",\"UNIT\":\"Beats/min\",\"FLAG\":null,\"CLI_INFO\":null,\"OPID\":\"150710\"}],\"sMSG\":null}"

//what should i do here?

如何拆分字符串并将其存储到 SQL Server 中?


这是 hl7View 类的代码:


public class hl7View

{

  public string sMSG {get;set;}

  public List<dataDB> Datalist {get;set;}

}

我创建的数据库。


我试图手动插入数据,它工作正常。是否有可能将其拆分,然后在反序列化后将其存储到数据库中?


江户川乱折腾
浏览 102回答 1
1回答

慕妹3146593

我认为您应该需要根据您创建的实体框架直接将dataDB列表对象添加到sql db。使用您已经拥有的 dataDB 列表对象设置 foreach 循环。dbcontext.tablename.add(item) // item 是 dataDB 的 foreach 循环对象。类似于下面的代码。 foreach (var mapping in mappings)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mapping.UserId = UserCommitteeMappingIDWiseUserID[mapping.UserCommitteeMappingID];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mapping.ModifiedBy = ModifiedBy;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mapping.ModifiedDate = DateTime.Now;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.UserCommitteeMappings.AddObject(mapping);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP