Loading

Manual JSON output

  1. Else
  2.         ' Fetch the field points results
  3.         ' Get the marker points and other JSON header info that's been built so far in to a string
  4.         strManualJSONHeader = oJSONResponse.JSONoutput()
  5.         ' Trim off the last bracket } and the crlf
  6.         strManualJSONHeader = Left(strManualJSONHeader, (LEN(strManualJSONHeader)-3))
  7.         ' Add a comma
  8.         strManualJSONHeader = strManualJSONHeader & "," & VbCrLf
  9.         ' Create the fieldPoints collection
  10.         strManualJSONHeader = strManualJSONHeader & "    ""fieldPoints"": [" & VbCrLf
  11.         ' Write out the header so far
  12.         Response.Write strManualJSONHeader
  13.         Response.Flush
  14.         ' Kill the oJSONResponse object as we don't need it after this point
  15.         Set oJSONResponse = nothing
  16.          ' Fetch the field points results and build the new data set
  17.         strSQL = "SELECT fields.field_id, fields.field_name, fields.field_area, fields_points.lat, fields_points.lng FROM fields INNER JOIN fields_points ON fields.field_id = fields_points.field_id WHERE (fields.farm_id = "& SuppliedFarmID &") ORDER BY id_field_point"
  18. 'response.write strSQL
  19. 'response.end
  20.         RS.Open strSQL, Conn, 0 ,1
  21.             ' Start writing out the data
  22.             x = 1
  23.             Do While NOT RS.EOF
  24.                 ' See if there is a decimal point in the results
  25.                 If InStr(RS.Fields.Item("field_area").Value, ".") > 0 Then
  26.                     ' Trim the fieldArea to two decimal places
  27.                     arrFieldSplit = Split(RS.Fields.Item("field_area").Value,".")
  28.                     ' ENSURE there are two decimal places in the JSON output to the user
  29.                     If LEN(arrFieldSplit(1)) = 0 Then
  30.                        strFieldArea = arrFieldSplit(0) & ".00"
  31.                     ElseIf LEN(Left(arrFieldSplit(1),2)) = 1 Then
  32.                         strFieldArea = arrFieldSplit(0) & "." & Left(arrFieldSplit(1),1) & "0"
  33.                     Else
  34.                         strFieldArea = arrFieldSplit(0) & "." & Left(arrFieldSplit(1),2)
  35.                     End If
  36.                 Else
  37.                     ' No decimal point found, lets add one
  38.                     strFieldArea = RS.Fields.Item("field_area").Value & ".00"
  39.                 End If
  40.                 ' Convert the string to a Double for JSON
  41.                 intFieldArea = CDbl(strFieldArea)
  42.                 ' Write out the instances to the collection
  43.                 Response.Write "       {" & VbCrLf
  44.                 Response.Write "           ""fieldName"": """ & RS.Fields.Item("field_name").Value & """," & VbCrLf
  45.                 Response.Write "           ""fieldID"": " & RS.Fields.Item("field_id").Value & "," & VbCrLf
  46.                 Response.Write "           ""fieldArea"": " & intFieldArea & "," & VbCrLf
  47.                 Response.Write "           ""lat"": """ & RS.Fields.Item("lat").Value & """," & VbCrLf
  48.                 Response.Write "           ""long"": """ & RS.Fields.Item("lng").Value & """" & VbCrLf
  49.                 ' See if this is the last one, if not then add a comma
  50.                 If x = intFieldPointsReturned Then
  51.                     Response.Write "       }" & VbCrLf
  52.                 Else
  53.                     Response.Write "       }," & VbCrLf
  54.                 End If
  55.                 Response.Flush
  56.                 x = x + 1
  57.                 RS.MoveNext
  58.             Loop
  59.         RS.Close
  60.         ' Close up the JSON for the fieldPoints collection
  61.         strManualJSONFooter = ""
  62.         strManualJSONFooter = strManualJSONFooter & "    ]" & VbCrLf
  63.         strManualJSONFooter = strManualJSONFooter & "}" & VbCrLf
  64.         ' Write out the footer
  65.         Response.Write strManualJSONFooter
  66.         Response.Flush
  67.         ' Close the DB and tidy up the objects
  68.         Conn.Close
  69.         Set RS2 = nothing
  70.         Set RS = nothing
  71.         Set Conn = nothing
  72.         Set strManualJSONHeader = nothing
  73.         Set strManualJSONFooter = nothing
  74.         ' END (this is poor - but was a quick fix)
  75.         Response.End
  76.     End If

Comments