-2

In my test i use PHP to convert the json string to insert it in the mysql database.

This is how the php file looks like:

if (isset($_GET['data'])) {  // Is er data binnengekomen, zoja voer dan de rest uit
$data = json_decode($_GET['data'], true); 
$id = $data['id'];                          //Het personeelsnummer
$latitude = $data['latitude'];              // De latitude (coordinaten)
$longitude = $data['longitude'];            // De longitude (coordinaten)
$timestamp = $data['timestamp'];            // De tijd

$result = mysql_query("SELECT * FROM locatie WHERE id='$id'");      //Kijken of het personeelsnummer al in de database staat of niet
if(mysql_num_rows($result)>0)                                       //Hier wordt de functie uitgevoerd als het al in de database staat
{
$query = "Update locatie                                            
SET     longitude = '$longitude',
   latitude = '$latitude',
   timestamp = '$timestamp'
WHERE id = '$id'";  

$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo "OK";

// Voer de $query(de Update Functie) uit
mysql_free_result($result); 

    }                               // Als het er in staat, Update alle waardes die voor het personeelsnummer gelden. Wordt dus geen geschiedenis opgeslagen
else                                                                    //Hier wordt de functie uitgevoerd als het nog niet in de database staat

{
//Als het een nieuw nummer is
$query = sprintf("INSERT INTO locatie (id, longitude, latitude, timestamp) VALUES ('%s', '%s', '%s', '%s') ", mysql_real_escape_string($id) , mysql_real_escape_string($longitude),    mysql_real_escape_string($latitude), mysql_real_escape_string($timestamp));}               // Als het er nog niet instaat voeg dan het personeelsnummer in en de andere waardes.         



$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo "OK";

// Voer de $query(de Insert Functie) uit
mysql_free_result($result);
}

This works perfectly fine, but the company requested that it should be in ASP.

But the point is, that im just bad at asp.

Here's my code:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Naamloos document</title>
</head>

<body>

    <% 
    Dim sConnection, objConn , objRS 

    sConnection = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; DATABASE=Your_Mysql_DB;   UID=mysql_username;PASSWORD=mysql_password; OPTION=3" 

    Set objConn = Server.CreateObject("ADODB.Connection") 

    objConn.Open(sConnection) 

    If Request.Form("data") <> "" Then

    Request.QueryString["id"] 
    Request.QueryString["longitude"] 
    Request.QueryString["latitude"] 
    Request.QueryString["timestamp"] 

    Set objRS = objConn.Execute("SELECT * FROM locatie WHERE id='id'")



    If objRS <> "" Then
    Set objRS = objConn.Execute("Update locatie                                             
    SET     longitude = 'longitude',
       latitude = 'latitude',
       timestamp = 'timestamp'
    WHERE id = 'id'")
    Else
    Set objRS = objConn.Execute("INSERT INTO locatie (id, longitude, latitude, timestamp)  VALUES ('id', 'longitude', 'latitude', 'timestamp') ")
    End If




    objRS.Close
    Set objRS = Nothing
    objConn.Close
    Set objConn = Nothing
    %>

    </body>
    </html>

I send this jsonstring:

2012-10-10 08:50:32.011 Appname[4049:c07] Post String =http://www.yourdomain.nl/locatie.asp?data=%7B%22id%22:%220612833397%22,%22longitude%22:%22-143.406417%22,%22latitude%22:%2232.785834%22,%22timestamp%22:%2210-10%2007:56%22%7D

EDIT

So my question is:

If the ASP works just as the PHP file. I dont know if i use the right methods to decode the json, if i use the right method if id=id in the database, if i use the right methos to parse it in the database

Rafael
  • 3,081
  • 6
  • 32
  • 53
David Raijmakers
  • 1,369
  • 1
  • 16
  • 40
  • @PaoloFalabella i just edited the question within my question - questionception... – David Raijmakers Oct 18 '12 at 12:40
  • The whole sql-related part in both PHP and ASP is not needed. Your question is "how to decode JSON in ASP". Show us only a part of ASP code and tell what have you tried from google results. – Piotr Gwiazda Oct 18 '12 at 16:35

2 Answers2

2

ASP is only subtle similar to php, but ASP has less extensions, API's etc.

By definition ASP (Classic-ASP) has been deprecated long time ago, and is only mantained in current windows versions only to comply a backward compatibility. but the language is not beign developed by micosoft anymore.

Said this you must know that ASP can't parse JSON per se, instead you need to implement a external library you can check Classic asp server-side JSON library for more information.

in other matters i notice you some errors that i have detected in your code:

  • in Request.QueryString[XXXXXX] [] characters are invalid use () instead.
  • you never close If Request.Form("data") <> "" Then
Community
  • 1
  • 1
Rafael
  • 3,081
  • 6
  • 32
  • 53
1

You are mixing up the Request.Form and Request.Querystring collection. Since in your example you send the data in the querystring (a GET) you should use the Request.Querystring collection.

Thus ln20 should be

If Request.Querystring("data") <> "" Then

instead of

If Request.Form("data") <> "" Then

It's also hard to evaluate if you json gets through fine.. You can test this, using a response.write Request.Querystring("data") to see the actual jsonstring. I'd recommend using jsonlint.com

Edit: Lastly, as Rafael in the other post mentioned you indeed need a third party library as there is no built-in json_decode analogue. I personally like the solution of SO post Any good libraries for parsing JSON in Classic ASP? where they suggest to use Douglas Crockford's json2.js javascript library serverside.

Community
  • 1
  • 1
AardVark71
  • 3,928
  • 2
  • 30
  • 50