将JSON字符串保存到SQL Server数据库的问题
问题描述:
我的Angular Web应用程序当前使用HTML5 localStorage
对象将JSON对象序列化为/从本地存储器反序列化JSON对象。我们现在试图保存并从SQL Server表中检索这些相同的JSON对象。将JSON字符串保存到SQL Server数据库的问题
但我有困难,因为我尝试使用以下SQL UPDATE
保存JSON字符串:
UPDATE [rz37883_SQLServer].[dbo].[RAGEDashboard]
SET
image = '{"widgets":[{"title":"Bar Chart","name":"chart_bar","style":{},"size":{"width":"50%","height":320},"dataModelOptions":{"title":{"text":"Bar Chart","position":"bottom"},"legend":{"visible":true,"position":"top"},"seriesDefaults":{"type":"bar","stack":false},"series":[{"field":"field1","name":"MTM"}],"dataSource":{"data":[{"field1":236151654.592439},{"field1":103612357.347808},{"field1":267066579.129913},{"field1":582355005.154486},{"field1":-9422699.958631}],"table":null},"valueAxis":{"labels":{"format":"{0:c}","rotation":-30},"line":{"visible":false},"axisCrossingValue":0},"categoryAxis":{"categories":["London","New York","Dubai","Paris","Stockholm"],"majorGridLines":{"visible":false},"labels":{"rotation":0,"margin":20}},"tooltip":{"visible":true,"template":"#= series.name #: #= kendo.format('{0:C0}', value) #"},"dimensions":[{"dimension":"BookingLocation","hierarchy":false,"id":0}],"riskMeasures":[{"name":"MTM","kri_group":"[MTM:MTM]","cube_vector":"MTM:MTM","aggreg_formula":"SUM(MTM:MTM)","id":0}]},"initImage":"images2/bar_chart.png","gadgetConfigured":true}]}'
WHERE [RAGEDashboardConfig_userid] = 'bobmazzo1234'
AND id = 1441288790
SQL Server错误消息是:
消息102,15级,状态1,第3行
'{'附近的语法不正确。
在角度指令代码中,item
对象当前被保存到localStorage
,如下所示。
save: function (widgets) {
//'widgets' are mapped to the 'serialized' var using _.map()
item = JSON.stringify(item);
this.storage.setItem(this.id, item);
}
我现在连接一个http请求,它将使用MS WebAPI调用到一个c#API层。
这里的问题是,我只是试图手动更新SQL Server表,所以我可以测试检索功能。
答
我认为这是因为你在字符串中有几个单引号。你必须逃避它们,具有讽刺意味的是,单引号就是逃避角色。
这里是轻微的修改我提出:
原文:kendo.format('{0:C0}', value)
修改:kendo.format(''{0:C0}'', value)
试着改变你的字符串,这一点,看看它是否修复该问题:
UPDATE [rz37883_SQLServer].[dbo].[RAGEDashboard]
SET
image =
'{"widgets":[{"title":"Bar Chart","name":"chart_bar","style":{},"size":{"width":"50%","height":320},"dataModelOptions":{"title":{"text":"Bar Chart","position":"bottom"},"legend":{"visible":true,"position":"top"},"seriesDefaults":{"type":"bar","stack":false},"series":[{"field":"field1","name":"MTM"}],"dataSource":{"data":[{"field1":236151654.592439},{"field1":103612357.347808},{"field1":267066579.129913},{"field1":582355005.154486},{"field1":-9422699.958631}],"table":null},"valueAxis":{"labels":{"format":"{0:c}","rotation":-30},"line":{"visible":false},"axisCrossingValue":0},"categoryAxis":{"categories":["London","New York","Dubai","Paris","Stockholm"],"majorGridLines":{"visible":false},"labels":{"rotation":0,"margin":20}},"tooltip":{"visible":true,"template":"#= series.name #: #= kendo.format(''{0:C0}'', value) #"},"dimensions":[{"dimension":"BookingLocation","hierarchy":false,"id":0}],"riskMeasures":[{"name":"MTM","kri_group":"[MTM:MTM]","cube_vector":"MTM:MTM","aggreg_formula":"SUM(MTM:MTM)","id":0}]},"initImage":"images2/bar_chart.png","gadgetConfigured":true}]}'
WHERE [RAGEDashboardConfig_userid]='bobmazzo1234' and id = 1441288790
哦所以一个JavaScript str.replace()函数,例如?或str.replace(/'/ g,“\\'”); –
我认为这可以工作!只要你不让它修改前导和尾随单引号。 – rwking