0

Im trying to create a json formated string in asp classic that I simply want to write on the page and then I get that in as a callback in my js file and parse it there and it works if I only use string text like this

response.write "{""Firstname"":""John"",""Lastname"":""Hogan""}"

But if I try to use variables with values from my db for the john an Hogan values something like this I can't get it to work?

fname="John" 
lname="Hogan" 
response.write "{""Firstname"":""""&fname&"""",""Lastname"":""""&lname&"""}"

Do I really need to use a json library like jsonObject.class.asp?

Any input really appreciated. Thanks!

Claes Gustavsson
  • 5,509
  • 11
  • 50
  • 86
  • You have got the quotes wrong. Forget json, think you are just concatenating strings to print. – Flakes Oct 11 '19 at 07:48
  • 2
    Sort out your quote escaping like @SearchAndResQ suggested should be `Response.Write "{""Firstname"":""" & fname & """, ""Lastname"":""" & lname & """}"`. Remember for every escaped quote you double it, but you still have to start and terminate a string using quotes when concatenating variables to them. – user692942 Oct 11 '19 at 08:15

1 Answers1

-4

I don't know if this is what you need. To my knowledge, you need a library 'cause native serialization is a bit tricky ^-^. The most used library is Newtonsoft lib (link)

I used it on an asp.net mvc5 web app. It is pretty easy to use, you have code samples on the website but there is an example with your code:

var json = new JObject {
  ["Firstname"] = fname,
  ["Lastname"] = lname
}

Or, you can use an object:

class Person {
    public string Firsname {get;set;}
    public string Lastname {get;set;}
}

// and in your code
var someone = new Person { Firstname = fname, Lastname = lname };
var json = JsonConvert.SerializeObject(someone );

Hope this will help you :)

Community
  • 1
  • 1
Zackna
  • 39
  • 1
  • 5
  • Classic ASP is not ASP.Net, c# or anything else, it uses Active Scripting languages like VBScript or JScript out of the box. – user692942 Oct 11 '19 at 08:13