我曾經在一個VB項目下面的類,它是一個ASMX服務(有點老土,我知道):API反序列化JSON在機能的研究,從VS工程/瀏覽器,而不是從jQuery的
Public Class cName
Public Property nameOrAlias As String
Public Property nameType As String
Public Property isVerified As String
Public Property nameID As String
Public Property icn As String
End Class
而且我用它在以下功能:
<WebMethod()>
Public Function SaveCaseNames(jsn As String) As String
Try
Dim json As String = jsn
Dim d As New DataTable
Dim c As New cShared
Dim s As New System.Web.Script.Serialization.JavaScriptSerializer
Dim sql As String = ""
Dim icn As String = ""
Dim sArgs As String = ""
Dim o As Object = s.Deserialize(Of Object)(json)
Dim js As New System.Web.Script.Serialization.JavaScriptSerializer
Dim lstNames = js.Deserialize(Of List(Of cName))(json)
'First we look for new names, those without a nameID
For Each cx As cName In lstNames
icn = cx.icn.ToString
If cx.nameID.ToString = "--" Then
sArgs &= "'" & icn & "', "
sArgs &= "'" & cx.nameOrAlias.ToString & "', "
sArgs &= "'" & cx.nameType.ToString & "', "
Select Case cx.isVerified.ToString
Case "YES"
sArgs &= "1"
Case "NO"
sArgs &= "0"
End Select
sql = "sp_insertCaseNames " & sArgs
d = c.GetDataTable(c.SetConnection(), sql)
d.TableName = "insertNames"
End If
Next
'if it has a D the entry should be deleted.
For Each cx As cName In lstNames
If cx.nameID.ToString.StartsWith("D") Then
Dim nameID As String = Replace(cx.nameID.ToString, "D", "")
sql = "UPDATE casenames Set st_id = 4 WHERE id = " & nameID
d = c.GetDataTable(c.SetConnection(), sql)
d.TableName = "deleteNames"
End If
Next
Return "HELLO WORLD!"
Catch ex As Exception
Debug.Print(ex.Message.ToString)
End Try
End Function
使用表格是有點滑稽,當然既然兩種數據調用的返回任何東西,但我不寫輔助功能尚未完成,在任何率不這個問題是關於什麼的。
如果我在VS 2015中運行該項目,它運行良好。我將以下JSON傳遞給上面的函數和所有作品。
[{"nameOrAlias":"TEST 4","nameType":"MIDDLE","isVerified":"YES","nameID":"16","icn":"N2016000020"},{"nameOrAlias":"TEST1","nameType":"AKA","isVerified":"YES","nameID":"D15","icn":"N2016000020"},{"nameOrAlias":"TESTXXX101","nameType":"AKA","isVerified":"YES","nameID":"17","icn":"N2016000020"},{"nameOrAlias":"HELLO JOE","nameType":"AKA","isVerified":"YES","nameID":"--","icn":"N2016000020"}]
刪除帶有「TEST1」的記錄並添加「HELLO JOE」。
當我打電話說從我的網頁使用jQuery完全相同的JSON數組,像這樣完全一樣的功能:
//Save the case names (aliases)
var jsonString = sessionStorage.getItem('caseNames');
var str = JSON.stringify(jsonString);
if (debug == 'true') {
alert(str);
}
$.ajax({
type: "POST",
url: "http://myserver.net/dps/svc/DeadSimple.asmx/SaveCaseNames",
data: str,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert('YAY!!!!!!!!!');
},
failure: function (response) {
alert('Fail! ' + response.d);
}
});
我得到一個錯誤信息:
{「消息」 :「無法將類型爲\ u0027System.String \ u0027的對象轉換爲 類型 \ System.Collections.Generic.IDictionary`2 [System.String,System.Object] \ u0027」,「StackTrace」:「 at System。 Web.Script.Serialization.ObjectConverter。 ConvertObjectToTypeInternal(Object o,Type type,JavaScriptSerializer serializer,Boolean throwOnError, Object \ u0026 convertedObject)\ r \ n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o,Type type,JavaScriptSerializer serializer,Boolean throwOnError, Object \ u0026 convertedObject)\ r \ n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize [T](String input)\ r \ n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall (HttpContext 上下文,WebServiceMethodData methodData)「,」ExceptionType「:」System.InvalidOperationException「}
我試過這個有沒有JSON.stringify
,沒有成功。我還包裝了數組,如:var str = JSON.stringify('{data:' + jsonString + '}');
,但這使得它更難解析,因爲現在我所追求的對象是更深層次的,並且它無法解決問題。
這是什麼造成的?如果我F5中的Web服務,並直接從瀏覽器調用它,它的工作原理。幫幫我!類庫的目標框架是3.5