Actually, the best solution is using JSON:
Documentation
JSON.parse(text[, reviver]);
Examples:
1)
var myobj = JSON.parse('{ "hello":"world" }');
alert(myobj.hello); // 'world'
2)
var myobj = JSON.parse(JSON.stringify({
hello: "world"
});
alert(myobj.hello); // 'world'
3) Passing a function to JSON
var obj = {
hello: "World",
sayHello: (function() {
console.log("I say Hello!");
}).toString()
};
var myobj = JSON.parse(JSON.stringify(obj));
myobj.sayHello = new Function("return ("+myobj.sayHello+")")();
myobj.sayHello();
Answer from Matej on Stack OverflowW3Schools
w3schools.com › jsref › jsref_object_tostring.asp
JavaScript Object toString() Method
The toString() method returns an object as a string.
W3Schools
w3schools.com › jsref › jsref_obj_string.asp
JavaScript String Reference
HTML Examples CSS Examples JavaScript Examples How To Examples SQL Examples Python Examples W3.CSS Examples Bootstrap Examples PHP Examples Java Examples XML Examples jQuery Examples · HTML Certificate CSS Certificate JavaScript Certificate Front End Certificate SQL Certificate Python Certificate PHP Certificate jQuery Certificate Java Certificate C++ Certificate C# Certificate XML Certificate ... W3Schools is optimized for learning and training.
Videos
14:36
Converting Objects to Strings with toString() - Advanced JavaScript ...
01:39
How to Convert a JavaScript Object to a String While Preserving ...
06:04
Mastering JavaScript Object Display: String Conversion, JSON & ...
16:28
Convert an object to a primitive - Basic JavaScript Fast (20) - ...
07:35
String Casting JavaScript - [Convert a Value to String in JavaScript] ...
02:45
How to Convert JavaScript Object to JSON String? - YouTube
Top answer 1 of 16
234
Actually, the best solution is using JSON:
Documentation
JSON.parse(text[, reviver]);
Examples:
1)
var myobj = JSON.parse('{ "hello":"world" }');
alert(myobj.hello); // 'world'
2)
var myobj = JSON.parse(JSON.stringify({
hello: "world"
});
alert(myobj.hello); // 'world'
3) Passing a function to JSON
var obj = {
hello: "World",
sayHello: (function() {
console.log("I say Hello!");
}).toString()
};
var myobj = JSON.parse(JSON.stringify(obj));
myobj.sayHello = new Function("return ("+myobj.sayHello+")")();
myobj.sayHello();
2 of 16
95
Your string looks like a JSON string without the curly braces.
This should work then:
obj = eval('({' + str + '})');
WARNING: this introduces significant security holes such as XSS with untrusted data (data that is entered by the users of your application.)
W3Schools
w3schools.com › js › js_json_stringify.asp
JSON.stringify()
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... A common use of JSON is to exchange data to/from a web server. When sending data to a web server, the data has to be a string. You can convert any JavaScript ...
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › toString
Object.prototype.toString() - JavaScript | MDN
... class Dog { constructor(name, ... code in place, any time an instance of Dog is used in a string context, JavaScript automatically calls the toString() method....
TutorialsPoint
tutorialspoint.com › home › javascript › javascript strings object
JavaScript Strings Object
September 1, 2008 - JavaScript strings can be created as objects using the String() constructor or as primitives using string literals. Use the following syntax to create a String object −
W3Schools Blog
w3schools.blog › home › javascript string object
Javascript String Object - W3schools
April 28, 2016 - A JavaScript String object represents a sequence of characters. 1. By using string literals. 2. By using String() constructor. When we create a string literal browser automatically converts it to a String object.
W3Schools
w3schools.com › jsref › jsref_string.asp
JavaScript String() Method
String() is an ECMAScript1 (JavaScript 1997) feature. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String
String - JavaScript | MDN
See "String primitives and String objects" below. String literals can be specified using single or double quotes, which are treated identically, or using the backtick character `. This last form specifies a template literal: with this form you can interpolate expressions. For more information on the syntax of string literals, see lexical grammar. There are two ways to access an individual character in a string.
W3Schools
w3schools.com › js › js_json_parse.asp
W3Schools.com
A common use of JSON is to exchange data to/from a web server. When receiving data from a web server, the data is always a string. Parse the data with JSON.parse(), and the data becomes a JavaScript object.
O'Reilly
oreilly.com › library › view › learning-javascript › 0596527462 › ch04s03.html
The String Object - Learning JavaScript [Book]
October 17, 2006 - <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Exploring String</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <script type="text/javascript"> //<![CDATA[ var sObj = new String( ); var sTxt = sObj.concat("This is a ", "new string"); document.writeln(sTxt); //]]> </script> </body> </html> There is no known limit to the number of strings you can concatenate with the String concat method. However, I rarely use this myself; I prefer the String operators, such as the string concatenation operator (+). The properties and methods available with the String object are listed in Table 4-1.
Author Shelley Powers
Published 2006
Pages 352
W3Schools
w3schools.com › jsref › jsref_tostring_string.asp
JavaScript String toString() Method
The toString() method is used by JavaScript when an object needs to be displayed as a text (like in HTML), or when an object needs to be used as a string. ... If you want to use W3Schools services as an educational institution, team or enterprise, ...
freeCodeCamp
forum.freecodecamp.org › javascript
Understanding strings as objects - JavaScript - The freeCodeCamp Forum
September 5, 2018 - I’m trying to better understand the internal structure of a string. In the following code, I initialize a string. To see what’s inside, I use Object.getOwnPropertyNames() and Object.values(). var str = 'hi'; console.log(Object.getOwnPropertyNames(str)); console.log(Object.values(str)); ...
Top answer 1 of 16
1667
I would recommend using JSON.stringify, which converts the set of the variables in the object to a JSON string.
var obj = {
name: 'myObj'
};
JSON.stringify(obj);
Most modern browsers support this method natively, but for those that don't, you can include a JS version.
2 of 16
182
Use javascript String() function
String(yourobject); //returns [object Object]
or stringify()
JSON.stringify(yourobject)
Top answer 1 of 3
2
Hi, · You would use JSON parse, like so: · var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}'); · This converts the text in to an object, which can then be used like... · document.getElementById("demo").innerHTML = obj.name + ", " + obj.age; · See link for more info: https://www.w3schools.com/js/js_json_parse.asp · Please mark reply as Helpful/Correct. Thanks! · Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
2 of 3
1
check this out · http://www.john-james-andersen.com/blog/service-now/converting-a-json-string-to-an-object-in-service... · · Hope this helps. · Mark my ANSWER as CORRECT n HELPFUL if it helped.
Programiz
programiz.com › javascript › examples › convert-object-string
JavaScript Program to Convert Objects to Strings
When using the String() function on an Object, the converted result will give [object Object].
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Working_with_objects
Working with objects - JavaScript | MDN
Notice the use of this to assign values to the object's properties based on the values passed to the function. Now you can create an object called myCar as follows: ... This statement creates myCar and assigns it the specified values for its properties. Then the value of myCar.make is the string "Eagle", myCar.model is the string "Talon TSi", myCar.year is the integer 1993, and so on.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › toString
String.prototype.toString() - JavaScript | MDN
const stringObj = new String("foo"); ... "foo" ... A string representing the specified string value. The String object overrides the toString method of Object; it does not inherit Object.prototype.toString()....