0

Im trying to parse a JSON response string that I get from a XmlHttp request in asp classic.

My problem is that my swedish characters ä becomes \xE4, and the same with å and ö.

Im using aspJSON.asp to parse it and Im setting the response.LCID = 1053 for swedish and including the aspJSON.asp and in short send and get the response back like this.

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="../../koppling.asp" -->
<!--#include file="aspJSON1.17.asp" -->
<%Response.LCID = 1053%>
<%
    Response.charset="utf-8"
    Response.CodePage = 65001 
    Set objXmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")

        objXmlHttp.Open "GET", url, False
        objXmlHttp.SetRequestHeader "Content-Type", "application/json"
        objXmlHttp.SetRequestHeader "User-Agent", "ASP/3.0"
        objXmlHttp.Send ()


    If objXmlHttp.Status = 200 Then

            jsonstring = CStr(objXmlHttp.ResponseText)
            'response.write jsonstring 
            Set oJSON = New aspJSON

            'Load JSON string
            oJSON.loadJSON(jsonstring)

            'getting the JSON value like this
            fnamn=oJSON.data("payment").item("consumer").item("privatePerson").item("firstName")

And when the fnamn has a åäö in it I can´t insert it into my db, because I get this error "Incorrect string value: '\xE4gen'" and its because it has an ä in the response.

So why isn´t it converting the character as it should, where do I go wrong?

Any input really appreciated, thanks.

EDIT

OK, so I added Response.CodePage = 65001 to the page and now the "fnamn" is writing the correct ä when I write response.write fnamn

But I still get this error when I try to insert it into my mysql db?

Microsoft OLE DB Provider for ODBC Drivers fel '80004005'

[MySQL][ODBC 3.51 Driver][mysqld-5.7.21-log]Incorrect string value: '\xE4gen' for column 'adress' at row 1

And this part \xE4 is the ä character.

And this is the hole son in the response I get back.

{"payment":{"paymentId":"51abf1fa7a014459b1e591dc534e9354","summary":{"reservedAmount":17000,"chargedAmount":17000},"consumer":{"shippingAddress":{"addressLine1":"Centralvägen","receiverLine":"Per Henrik Nordin","postalCode":"83162","city":"ÖSTERSUND","country":"SWE"},"company":{"contactDetails":{"phoneNumber":{}}},"privatePerson":{"firstName":"Per Henrik","lastName":"Nordin","email":"info@manmade.se","phoneNumber":{"prefix":"+46","number":"702315096"}}},"paymentDetails":{"paymentType":"CARD","paymentMethod":"Visa","invoiceDetails":{},"cardDetails":{"maskedPan":"492500******0004","expiryDate":"0320"}},"orderDetails":{"amount":17000,"currency":"SEK","reference":"860153"},"checkout":{"url":"https://www.manmade.se/dumpen/index.asp"},"created":"2019-05-19T04:19:31.5198+00:00","charges":[{"chargeId":"1306fbec6fcd4d68a3681bd16f9de1ec","amount":17000,"created":"2019-05-19T06:19:52.8161+02:00","orderItems":[{"reference":"2-1530","name":"Svart Budda huvud","quantity":1.0,"unit":"st","unitPrice":12800,"taxRate":2500,"taxAmount":4200,"grossTotalAmount":17000,"netTotalAmount":12800}]}]}}

And the 2 problems are addressLine1 and city in the above, since they have ä and Ö in them. And I get the values with this.

adress=oJSON.data("payment").item("consumer").item("shippingAddress").item("addressLine1")

postadress=oJSON.data("payment").item("consumer").item("shippingAddress").item("city")

And I then try to insert them with this sql.

sql= "INSERT INTO ordrar(userId,appusers_id,paymentId,produkt_id,produkter_id,ordernummer,antal,pris,fraktkostnad,artikel,texten,artikelnummer,farg,fargnummer,storlek,datum,betald,annonser_id,korttyp,kortnr,chargeId,big_image,big_image2,big_image3,big_image4,big_image5,betaltbelopp,email,fnamn,enamn,adress,postnr,postadress,mobil,annonstyp,annonsstatus) VALUES ("&rs("userId")&","& appusersId &",'"& dibspaymentId &"',"& rs("produkt_id") &","& rs("produkter_id") &",'"& dibsordernr &"',"& rs("antal") &","& rs("pris") &","& rs("fraktkostnad") &",'"& rs("artikel") &"','"& rs("texten") &"','"& rs("artikelnummer") &"','"& rs("farg") &"','"& rs("fargnummer") &"','"& rs("storlek") &"','"& rs("datum") &"','"&betald&"',"& rs("annonser_id") &",'"&dibspaymentMetod&"','"&dibskortNr&"','"&dibschargeId&"','"& rs("big_image") &"','"& rs("big_image2") &"','"& rs("big_image3") &"','"& rs("big_image4") &"','"& rs("big_image5") &"','"& dibssumma &"','"& email &"','"& fnamn &"','"& enamn &"','"& adress &"','"& postnr &"','"& postadress &"','"& mobil &"','"& annonstyp &"','ejgranskad');"

But since it displays the ä correctly when I write response.write adress, I don´t understand why it says that it is Incorrect string value: '\xE4gen' for column 'adress' ?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Claes Gustavsson
  • 5,509
  • 11
  • 50
  • 86
  • Have you seen this? -> [“Incorrect string value” when trying to insert UTF-8 into MySQL via JDBC?](https://stackoverflow.com/questions/10957238/incorrect-string-value-when-trying-to-insert-utf-8-into-mysql-via-jdbc). It is for jdbc, but maybe same thing applies here? – Flakes May 19 '19 at 17:07
  • 1
    Thanks, but I have set my db, tables, columns to utf8mb4_general_ci, and I can store emojis in it so it works as it should...I think ;-) – Claes Gustavsson May 19 '19 at 17:32
  • 1
    I have this in my mysql configuration file, my.ini character-set-server=utf8mb4 collation-server = utf8mb4_unicode_ci init-connect='SET NAMES utf8mb4' init_connect='SET collation_connection = utf8mb4_unicode_ci' skip-character-set-client-handshake – Claes Gustavsson May 19 '19 at 18:15

0 Answers0