如何在asp.NET2008中创建json php数组结构
如何在dotnet 3.5中json序列化此数组结构如何在asp.NET2008中创建json php数组结构,asp.net,json,vb.net,Asp.net,Json,Vb.net,如何在dotnet 3.5中json序列化此数组结构 <?php $response = array( 'file_version' => 2, 'files' => array( array( 'file_name' => 'test1.exe', 'url' => 'http://127.0.0.1/heartkey/files/test1
<?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;
Web Forms.aspx不是生成JSON的好技术,例如用于浏览器AJAX调用的JSON,因为它的页面生命周期很长,并且往往会继承大量需要从响应中剥离的环境开销母版页、标题等
REST/JSON类型服务的首选当代技术是,但不幸的是,这不适用于.NET3.5,仅适用于4.0及更高版本
您可以改为创建一个.ASMX Web服务,并使用它公开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调用您的PHP代码正在将您的对象编码为Json对象,这就是您要寻找的吗?@user3401991它当然已编译,尽管我最初的回答返回了一个字符串。我已经上传了一个完整的示例作为GitHub Gist-将所有文件复制到同一个文件夹并构建它。default.aspx上有一个Ajax客户端示例-请注意无关的d
<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