![]() |
标准的JSON
作者:jangogo 更新时间:2012/5/7 17:44:00
JSON数据格式应用非常广泛,是一种轻量级的数据交互格式,是“键值对(Key - Value)”数据的最佳选择。
在Javascript中 编写 一个 JS对象有多种写法(js 的语法灵活,要求不严格):
{a : 'abc'}
{'a' : 'abc'}
{a : "abc"}
{"a" : "abc"}
以上几种的js 中都能行得通,但是在其他语言进行解析时候也不一定能行得通,原因是为了规范起见,国际标准化组织(没有记错的话?)规定了“标准的JSON”格式,要求必须 只能使用双引号作为键 或者 值的边界符号,不能使用单引号,而且“键”必须使用边界符(双引号)。上述四种写法中,只有最后一种是标准的。其他的都不对(会存在一定的兼容性风险)。
建议以后大家都使用 标准的 JSON 格式
同时注意, jquery 的1.4版本在一些只带JSON解析器的浏览器中也存在这个问题,请务必尝试 使用 window.JSON=null 把浏览器自带的JSON解析器禁用
.Net 的JSON库(对象的序列号和反序列化),只支持标准JSON(遗憾!):
<%@ Page Language="vb" AutoEventWireup="false"%>
<script runat="server">
'-<TABLE prop="{module:'accounting',ttype:'ledger',tname:'gl',version:'1',title:'三栏帐',uid:'',src:''}"
<Runtime.Serialization.DataContract()> _
Public Class clsProp
Dim s_module As String
<Runtime.Serialization.DataMember()> _
Public Property [module]() As String
Get
Return s_module
End Get
Set(ByVal value As String)
s_module = value
End Set
End Property
Dim s_ttype As String
<Runtime.Serialization.DataMember()> _
Public Property ttype() As String
Get
Return s_ttype
End Get
Set(ByVal value As String)
s_ttype = value
End Set
End Property
Dim s_tname As String
<Runtime.Serialization.DataMember()> _
Public Property tname() As String
Get
Return s_tname
End Get
Set(ByVal value As String)
s_tname = value
End Set
End Property
Dim s_title As String
<Runtime.Serialization.DataMember()> _
Public Property title() As String
Get
Return s_title
End Get
Set(ByVal value As String)
s_title = value
End Set
End Property
Dim s_uid As String
<Runtime.Serialization.DataMember()> _
Public Property uid() As String
Get
Return s_uid
End Get
Set(ByVal value As String)
s_uid = value
End Set
End Property
Dim s_src As String
<Runtime.Serialization.DataMember()> _
Public Property src() As String
Get
Return s_src
End Get
Set(ByVal value As String)
s_src = value
End Set
End Property
Dim bNotSaveToDB As Boolean
<Runtime.Serialization.DataMember()> _
Public Property notSaveToDB() As Boolean
Get
Return bNotSaveToDB
End Get
Set(ByVal value As Boolean)
bNotSaveToDB = value
End Set
End Property
End Class
</script>
<%
'On Error Resume Next
''这个功能页面用于发布前对要发布的文件进行预处理
Dim fn As String
fn = LCase("D:\4Fang Web\apps\SaaS\template\cn\accounting\gl.xml") ' LCase(Request.QueryString("source")) ''正要发布的文件
Dim appRootPath As String
appRootPath = Mid(Request.PhysicalPath, 1, InStrRev(Request.PhysicalPath, "\") - 1)
appRootPath = Mid(appRootPath, 1, InStrRev(appRootPath, "\"))
'Response.Write(appRootPath)
''1.发布前要对模板文件进行一个更新,添加一个f属性表示它文件的原始位置
Dim templatePath As String
templatePath = LCase(appRootPath & "template\")
If Right(fn, 4) = ".xml" And Left(fn, Len(templatePath)) = templatePath Then
Dim xD As New System.Xml.XmlDocument
xD.Load(fn)
Dim nds As System.Xml.XmlNodeList
nds = xD.GetElementsByTagName("TABLE")
Dim sProp As String = vbNullString
Dim att As System.Xml.XmlAttribute = nds(0).Attributes("prop")
If att Is Nothing = False Then
sProp = att.Value
End If
'If nds.Count > 0 Then
' nds = xD.GetElementsByTagName("root")
' If nds.Count = 1 Then
' nds(0).Attributes.Append(xD.CreateAttribute("f"))
' nds(0).Attributes("f").Value = Right(fn, Len(fn) - Len(templatePath))
' Response.Write(xD.OuterXml)
' End If
'End If
If sProp <> vbNullString Then
Dim objProp As New clsProp
Dim ms As New System.IO.MemoryStream(Encoding.UTF8.GetBytes(sProp))
Dim ser As New System.Runtime.Serialization.Json.DataContractJsonSerializer(objProp.GetType())
objProp = TryCast(ser.ReadObject(ms), clsProp)
ms.Close()
Response.Write(objProp.tname)
End If
End If
%>