2014-12-31 36 views
1

如何在jQuery 3.5中串行化這個數組結構?如何在asp.net 2008中使用json php數組結構

<?php 

$response = array(
    'file_version' => 2, 
    'files' => 
     array(
      array(
       'file_name' => 'test1.exe', 
       'url' => 'http://127.0.0.1/heartkey/files/test1.exe', 
       'path' => 'images\filename\\' 
      ), 
      array(
       'file_name' => 'test2.exe', 
       'url' => 'http://127.0.0.1/heartkey/files/test2.exe', 
       'path' => 'images\filename\\' 
      ), 
      array(
       'file_name' => 'test3.exe', 
       'url' => 'http://127.0.0.1/heartkey/files/test3.exe', 
       'path' => 'images\filename\\' 
      ) 
     ), 
    'files_max_size' => 3000 
); 

$json = json_encode($response); 

echo $json; 

我發現基本方式,但我不知道如何表示,在VB中, 我用數組和嵌套的字典,但沒有運氣tryed。 我在vb.net的知識是最基本的。

這是我有:

Imports System.Web.Script.Serialization 

Partial Class _Default 
    Inherits System.Web.UI.Page 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load 
     Dim info As Dictionary(Of String, Dictionary(Of String, String)) 

     'info.Add("files", New Dictionary(Of String, String)) 

     Dim serializer As New JavaScriptSerializer() 
     Dim serializedResult = serializer.Serialize(info) 

     Response.Write(serializedResult) 
    End Sub 
End Class 
+0

您的PHP代碼編碼的對象一個JSON對象,是什麼 – ImadT

回答

0

Web窗體(.aspx)不產生JSON例如一項偉大的技術供瀏覽器AJAX調用使用,因爲它的頁面生命週期很長,並且傾向於繼承很多環境開銷(主頁面,頭部等),這些開銷需要從響應中剝離出來。

REST/JSON類型服務的首選當代技術是Microsoft WebAPI,但不幸的是,這不適用於.Net 3.5,僅4.0和更高版本。

你可以做的反而是創建一個.ASMX Web服務,並使用WebMethods暴露您的JSON序列化字符串:

<System.Web.Script.Services.ScriptService()> _ 
Public Class WebService1 
    Inherits System.Web.Services.WebService 

    <System.Web.Services.WebMethod()> _ 
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _ 
    Public Function RenderJson() As Object 
     Dim theObjects() = { 
      New With { 
       .file_name = "test1.exe", 
       .url = "http://127.0.0.1/heartkey/files/test1.exe", 
       .path = "images\filename\\" 
       }, 
      New With { 
       .file_name = "test2.exe", 
       .url = "http://127.0.0.1/anotherUrl", 
       .path = "images\foo" 
       } 
      } 
     Return theObjects 
    End Function 

我在這裏用匿名類,但你也可以使用強命名類保存你的數據。 (我不認爲我的JavaScript對象的形狀非常正確,但你明白了)。

編輯 - 被錯誤地返回一個字符串:( 我貼一個完整的工作示例包括客戶端AJAX調用了一個GitHub gist

+0

你的示例代碼有很多語法錯誤 – user3401991

+0

@ user3401991雖然我的原始答案返回了一個字符串,但我已經上傳了一個完整的示例作爲GitHub Gist - 將所有文件複製到相同的文件夾並構建它。在default.aspx上有一個Ajax客戶端示例 - 注意多餘的'd' [根對象](http://encosia.com/a-breaking-change-between-versions-of-aspnet -ajax /) – StuartLC

相關問題