Json到SQL ASP经典

Json到SQL ASP经典,json,vbscript,asp-classic,fetch,Json,Vbscript,Asp Classic,Fetch,无法用ASP Classic解决此问题,外部服务器将向我的站点发送一些Json 例如:www.mypage.com/getjson.asp {Userid:112233,成员:Tom} 我如何获取这个并检查用户ID是否已经存在,否则将其放入SQL中 如果用户ID存在,我只需回答 response.Write{Userid:true} 我能把它转换成一个查询字符串或类似的东西吗 编辑: 谢谢你的帮助,但我不想让它与本帖中的提示一起工作 但是我用这个代码得到了整个字符串 bytecount = Re

无法用ASP Classic解决此问题,外部服务器将向我的站点发送一些Json 例如:www.mypage.com/getjson.asp

{Userid:112233,成员:Tom} 我如何获取这个并检查用户ID是否已经存在,否则将其放入SQL中

如果用户ID存在,我只需回答

response.Write{Userid:true} 我能把它转换成一个查询字符串或类似的东西吗

编辑: 谢谢你的帮助,但我不想让它与本帖中的提示一起工作 但是我用这个代码得到了整个字符串

bytecount = Request.TotalBytes
bytes = Request.BinaryRead(bytecount)

Set stream = Server.CreateObject("ADODB.Stream")
    stream.Type = 1              
    stream.Open()                                   
        stream.Write(bytes)
        stream.Position = 0                             
        stream.Type = 2                 
        stream.Charset = "utf-8"                      
        s = stream.ReadText()               
    stream.Close()
Set stream = nothing
Response.Write =(s)
但现在我需要考虑如何清理和分割, 请注意,成员没有qoutes

{"Userid":"00004547552009","Member":1,"id":"0060a80040d9"}

为此,您确实需要一个JSON传递程序。我发现的最好的一个类是VbsJson类,它有一个将JSON解析为VBScript的解码方法和一个从VBScript生成JSON的编码方法。代码有点长,我提取了编码和解码功能并使用了它。它似乎工作得很好

我感觉你有一个简单的JSON,它总是一样的。当然使用不同的值,但始终使用用户ID和成员。如果是这样的话,这里有一些对你有用的东西。注意-您的示例中缺少Tom的引号。将其保存到一个名为json.txt的txt文件中,该文件与.asp页面位于同一目录中:

{Userid:112233,成员:Tom} 现在把这个放到你的页面上。它应该被剪切或粘贴,除非它希望您有一个名为conn的数据库连接对象。哦,您必须更改查询

dim fso、jsonStr、myArray、i、sql、rs 设置fso=Server.CreateObjectScripting.Filesystemobject '从某个源读取json。在这种情况下,它与.asp文件位于同一位置的txt文件中 jsonStr=fso.OpenTextFileServer.MapPathjson.txt.readAll “在桌子上分开 myArray=splitjsonStr, '通过元素循环 对于i=0到uBoundmyArray “检查哪一个是数字 如果是NumericMyarayi那么 response.write myArrayi&是一个数字,因此请签入DB sql=从表中选择id,其中userID=&myArrayi&; 设置rs=conn.executesql 如果是rs.eof那么 '不在DB中,因此返回JSON response.Write{Userid:true} 如果结束 其他的 'response.write myArrayi&不是数字 如果结束 下一个 更新-正如我所说的,上面的解决方案是为JSON格式定制的,您知道它不会改变,非常简单,并且您知道一个整数值就是您要寻找的那个。您应该使用JSON paser来处理其他任何事情

您说您不确定JSON是如何发送的。它是一个文本字符串,将通过请求对象发送并提供给您。.form或.querystring。我总是尽量避免在任何情况下发牢骚。由于许多原因,表单集合比整个对话更好。没有人不能像你的例子那样传递JSON。我认为必须对其进行编码,例如:

{"Userid":"112233"}
看起来是这样的:

www.mypage.com/path.asp?json=%7B%22Userid%22%3A%20112233%7D

为此,您确实需要一个JSON传递程序。我发现的最好的一个类是VbsJson类,它有一个将JSON解析为VBScript的解码方法和一个从VBScript生成JSON的编码方法。代码有点长,我提取了编码和解码功能并使用了它。它似乎工作得很好

我感觉你有一个简单的JSON,它总是一样的。当然使用不同的值,但始终使用用户ID和成员。如果是这样的话,这里有一些对你有用的东西。注意-您的示例中缺少Tom的引号。将其保存到一个名为json.txt的txt文件中,该文件与.asp页面位于同一目录中:

{Userid:112233,成员:Tom} 现在把这个放到你的页面上。它应该被剪切或粘贴,除非它希望您有一个名为conn的数据库连接对象。哦,您必须更改查询

dim fso、jsonStr、myArray、i、sql、rs 设置fso=Server.CreateObjectScripting.Filesystemobject '从某个源读取json。在这种情况下,它与.asp文件位于同一位置的txt文件中 jsonStr=fso.OpenTextFileServer.MapPathjson.txt.readAll “在桌子上分开 myArray=splitjsonStr, '通过元素循环 对于i=0到uBoundmyArray “检查哪一个是数字 如果是NumericMyarayi那么 response.write myArrayi&是一个数字,因此请签入DB sql=从表中选择id,其中userID=&myArrayi&; 设置rs=conn.executesql 如果是rs.eof那么 '不在DB中,因此返回JSON response.Write{Userid:true} 如果结束 其他的 'response.write myArrayi&不是数字 如果结束 下一个 更新-正如我所说的,上面的解决方案是为JSON格式定制的,您知道它不会改变,非常简单,并且您知道一个整数值就是您要寻找的那个。您应该使用JSON paser来处理其他任何事情

您说您不确定JSON是如何发送的。它是一个文本字符串,将通过请求对象发送并提供给您。.form或.querystring。我总是回避质疑 字符串,如果我可以在所有情况下。由于许多原因,表单集合比整个对话更好。没有人不能像你的例子那样传递JSON。我认为必须对其进行编码,例如:

{"Userid":"112233"}
看起来是这样的:

www.mypage.com/path.asp?json=%7B%22Userid%22%3A%20112233%7D

经典ASP支持Javascript和VBScript,根据我的经验,使用它来处理JSON要比使用任何JSON VBS类容易得多。您可以在页面顶部声明Javascript作为您的语言,但是如果您喜欢在其他地方使用VBS,您可以在页面的头部使用类似的内容。注意runat=Server属性

<script language="javascript" runat="server">
var JSONObject= Request("YourJsonStringName");
var jUserId = JSONObject.UserId;
var jMember =JSONObject.Member;
</script>

然后,字符串jUserId和jMember将可供您在页面中的其他位置在VBS代码内的分隔符中使用,您可以在数据库插入中使用它们

Classic ASP支持Javascript和VBScript,根据我的经验,使用它处理JSON要比使用任何JSON VBS类容易得多。您可以在页面顶部声明Javascript作为您的语言,但是如果您喜欢在其他地方使用VBS,您可以在页面的头部使用类似的内容。注意runat=Server属性

<script language="javascript" runat="server">
var JSONObject= Request("YourJsonStringName");
var jUserId = JSONObject.UserId;
var jMember =JSONObject.Member;
</script>

然后,字符串jUserId和jMember将可供您在页面中的其他位置在VBS代码内的分隔符中使用,您可以在数据库插入中使用它们

Mate,我建议您检查ASP JSON类,它可以使您的代码干净,易于编码。检查:


干杯。

伙计,我建议你检查一下ASP JSON类,它可以让你的代码变得干净,并且很容易编码。检查:


干杯。

那么使用我的实用程序类呢:

自述文件中的一些示例:

' instantiate the class
Dim oJSON = New JSON

' add properties
oJSON.Add "prop1", "someString"
oJSON.Add "prop2", 12.3
oJSON.Add "prop3", Array(1, 2, "three")

' change some values
oJSON.Change "prop1", "someOtherString"
oJSON.Change "prop4", "thisWillBeCreated" ' this property doen't exists and will be created automagically

' get the values
Response.Write oJSON("prop1") & "<br>"
Response.Write oJSON("prop2") & "<br>"
Response.Write oJSON("prop3") & "<br>"
Response.Write oJSON("prop4") & "<br>"

' get the JSON formatted output
Dim jsonSting
jsonString = oJSON.Serialize() ' this will contain the string representation of the JSON object

oJSON.Write() ' this will write the output to the Response - equivalent to: Response.Write oJSON.Serialize()

' load and parse some JSON formatted string
jsonString = "{ ""strings"" : ""valorTexto"", ""numbers"": 123.456, ""arrays"": [1, ""2"", 3.4, [5, 6, [7, 8]]], ""objects"": { ""prop1"": ""outroTexto"", ""prop2"": [ { ""id"": 1, ""name"": ""item1"" }, { ""id"": 2, ""name"": ""item2"", ""teste"": { ""maisum"": [1, 2, 3] } } ] } }" ' double quotes here because of the VBScript quote scaping

oJSON.Parse(jsonString) ' set this to a variable if your string to load can be an Array, since the function returns the parsed object and arrays are parsed to JSONarray objects
' if the string represents an object, the current object is returned so there is not need to set the return to a new variable

oJSON.Write()

那么如何使用我的实用程序类:

自述文件中的一些示例:

' instantiate the class
Dim oJSON = New JSON

' add properties
oJSON.Add "prop1", "someString"
oJSON.Add "prop2", 12.3
oJSON.Add "prop3", Array(1, 2, "three")

' change some values
oJSON.Change "prop1", "someOtherString"
oJSON.Change "prop4", "thisWillBeCreated" ' this property doen't exists and will be created automagically

' get the values
Response.Write oJSON("prop1") & "<br>"
Response.Write oJSON("prop2") & "<br>"
Response.Write oJSON("prop3") & "<br>"
Response.Write oJSON("prop4") & "<br>"

' get the JSON formatted output
Dim jsonSting
jsonString = oJSON.Serialize() ' this will contain the string representation of the JSON object

oJSON.Write() ' this will write the output to the Response - equivalent to: Response.Write oJSON.Serialize()

' load and parse some JSON formatted string
jsonString = "{ ""strings"" : ""valorTexto"", ""numbers"": 123.456, ""arrays"": [1, ""2"", 3.4, [5, 6, [7, 8]]], ""objects"": { ""prop1"": ""outroTexto"", ""prop2"": [ { ""id"": 1, ""name"": ""item1"" }, { ""id"": 2, ""name"": ""item2"", ""teste"": { ""maisum"": [1, 2, 3] } } ] } }" ' double quotes here because of the VBScript quote scaping

oJSON.Parse(jsonString) ' set this to a variable if your string to load can be an Array, since the function returns the parsed object and arrays are parsed to JSONarray objects
' if the string represents an object, the current object is returned so there is not need to set the return to a new variable

oJSON.Write()

好的,很长一段时间之后…只有一个部分答案,用于解析包含JSON的URL查询。一旦发生这种情况,SQL就变得简单了

这个答案使用Jquery——这取决于Jquery在通过.get发送之前如何转换JSON

我之所以发布这篇文章,是因为约翰已经很简单的答案的一个简单版本对我有用。它只是键值URL对

还有-警告-尽管John的示例进行了一些修改,使提到的库变得不必要,但我不能保证这种方法适用于更复杂的JSON-我还没有尝试过。另外,我很高兴他提到将服务器端jscript与asp的vbscript混合使用,这是一种我已经忘记的有用技术

客户端代码从

注意使用.get not.post:

正确地说,UserID、Name和City应该用引号括起来,但在本例中,任何一种方式都可以正确解析

以下是基于John答案的服务器端代码:

<script language="javascript" runat="server">
   //// Yes, ok, it's jscript, not really javascript - but it works 
   //// ... probably just as easy to use ASP-vbscript
   var jUserId = Request("UserId");
   var jName = Request("Name"); 
   var jCity = Request("City");
</script>

<%
   response.write ("You sent: " & request.ServerVariables("QUERY_STRING") ) '' verbose
   response.write ("'jscript' version got: " & jUserId & " " & jName & " " & jCity)
%>

好的,很长一段时间之后…只有一个部分答案,用于解析包含JSON的URL查询。一旦发生这种情况,SQL就变得简单了

这个答案使用Jquery——这取决于Jquery在通过.get发送之前如何转换JSON

我之所以发布这篇文章,是因为约翰已经很简单的答案的一个简单版本对我有用。它只是键值URL对

还有-警告-尽管John的示例进行了一些修改,使提到的库变得不必要,但我不能保证这种方法适用于更复杂的JSON-我还没有尝试过。另外,我很高兴他提到将服务器端jscript与asp的vbscript混合使用,这是一种我已经忘记的有用技术

客户端代码从

注意使用.get not.post:

正确地说,UserID、Name和City应该用引号括起来,但在本例中,任何一种方式都可以正确解析

以下是基于John答案的服务器端代码:

<script language="javascript" runat="server">
   //// Yes, ok, it's jscript, not really javascript - but it works 
   //// ... probably just as easy to use ASP-vbscript
   var jUserId = Request("UserId");
   var jName = Request("Name"); 
   var jCity = Request("City");
</script>

<%
   response.write ("You sent: " & request.ServerVariables("QUERY_STRING") ) '' verbose
   response.write ("'jscript' version got: " & jUserId & " " & jName & " " & jCity)
%>

将它放在本地文件中真的吗?动态生成字符串同样快。@user3526720 Mat41为您提供了一个很好的起点,至少可以尝试编写一些东西。是的,类似的东西,但是我没有本地文件中的Json数据。我如何从url读取外部服务器中的日志文件看起来像www.mypage.com/getjson.asp Userid=112233 Member=t你是说Userid和Member不是Json字符串,而是querystring值?它是Json,但是日志看起来像一个查询字符串,可能只是为了显示其中的内容,将其放在本地文件中,真的吗?动态构建字符串也一样快。@user3526720 Mat41为您提供了一个很好的起点,至少可以尝试编写一些东西。是的,类似的东西,但是我没有本地文件中的Json数据。我如何从url读取外部服务器中的日志文件看起来像www.mypage.com/getjson.asp Userid=112233 Member=t你是说Userid和Member不是Json字符串,而是querystring值?它是Json,但是日志看起来像一个查询字符串,可能只是为了显示itIt支持的内容,而不是不顾语言中使用javascript的事实
激活。@Lankymart&John-见下面我的答案。谢谢约翰,谢谢我提到的几件事。您的答案非常简单有效,我不得不对此进行扩展。它支持javascript,而不管语言指令中使用了javascript。@Lankymart&John-请参见下面我的答案。谢谢约翰,谢谢我提到的几件事。你的答案很简单,很有效,我不得不对此进行扩展。这看起来是个不错的选择。我在上面的帖子中使用了我喜欢的极端JSON,我下次会试试。这看起来是个不错的选择。我在上面的帖子中使用了类似的极端JSON,下次我会尝试使用它。服务器端jscript似乎可以很好地处理IIS7及以上版本的JSON。根据jscript上的Wikipedia条目,当前Windows服务器上的版本似乎是Internet Explorer中包含的版本9@John-的确-它非常方便,但您必须注意脚本加载顺序!服务器端jscript似乎可以很好地处理IIS7及以上版本的JSON。根据jscript上的Wikipedia条目,当前Windows服务器上的版本似乎是Internet Explorer中包含的版本9@John-的确-它非常方便,但您必须注意脚本加载顺序!