how to convert a string to an object in Javascript? [duplicate] - javascript

This question already has answers here:
Safely turning a JSON string into an object
(28 answers)
Closed 8 years ago.
I would like to know how to convert the string below to an object in Javascript:
var string = '{"id":"50","actor":"1","subject":"1","object":"18","message":"a ajout\u00e9","status":"unseen"}'
In fact, I would like that after converting the string above to an object, I will be able to access to any element of that object. For example, if I convert the string to an object called "obj" I will be able to access to the value of the element "id" (which is "50") in such way: obj.id
Thanks in advance.

This string is in JSON.
Use
var obj = JSON.parse(string);

Related

Parsing and reformatting an object to a simple javascript array [duplicate]

This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 1 year ago.
Suppose I have an object from that has a key and value pair and I want to reformat it into a simple javascript array:
var obj = {STRUCTURE_: '004A006', SEG_ID: '04081591__,0.12,0.25,65850'}
that looks like this:
['004A006', '04081591__,0.12,0.25,65850']
Whats the easiest way to do that?
Use Object.values to get the object's enumerable property values:
var obj = {STRUCTURE_: '004A006', SEG_ID: '04081591__,0.12,0.25,65850'}
const res = Object.values(obj)
console.log(res)

How to get the keyword of a json varible? [duplicate]

This question already has answers here:
How to get all key in JSON object (javascript)
(7 answers)
Closed 2 years ago.
I have a varible, if I print it out, I see this output:
Well, if I'd know the number '118', it would be easy, but in the program where I am using it, I don't know it. So is there any mode to get it without knowing that value?
You could use the Object.keys function to retrieve all keys of your object.
The Object.keys returns an array, you can then access the first element of that array like any other array.
const JSONString = '{"118": {"input1": 6, "input2": 1, "input3": 3}}';
const json = JSON.parse(JSONString);
const keys = Object.keys(json);
console.log(keys[0]);

Setting JSON key with indexed array value [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 4 years ago.
I am trying to populate a JSON object by automatically setting the keys for an JSON object based on the string value of another array. For example,
var test = ["a","b"]
{test[0]:"A"}
However, I get a Syntax error when I do this, if I manually set the value as the string as shown in the third line {"a":"A"} this issue does not happen. I've checked that test[0] does indeed print out "a" and its datatype is a string. Is there any reason why this might be happening?
Try the following:
var test = ["a","b"]
var obj = {
[test[0]]:"A"
};
console.log(obj);

How can I get a specific value from a JSON string in JavaScript? [duplicate]

This question already has answers here:
Safely turning a JSON string into an object
(28 answers)
Closed 7 years ago.
I have a JavaScript object named page.cat.lastResponseText.
This object has the following string value:
{
"orderBy":"",
"orderDesc":false,
"rows":[
{"catIndId":"3","indId":"1","catId":"2"},
{"catIndId":"4","indId":"4","catId":"2"}
],
"totalResults":2,
"totalPages":1,
"pageSize":2,
"currentPage":1
}
How I can convert this string into a JSON object? And how do I then get the values for object such as cat.rows or cat.totalResults?
Simply use JSON.stringify() and JSON.parse()
http://caniuse.com/#feat=json

How to access object using dynamic key? [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 9 years ago.
How to access an object using a variable as key. Here is my code sample:
var o = {"k1": "111", "k2": "222"};
alert(o.k1); //working fine
var key = "k"+1; alert(key); // k1
alert(o.key); //not working
You can access objects like arrays:
alert(o[key]);
Change the last line to: alert(o['k1']); or alert(o[key]); where key is your dynamically constructed property key.
Remember you can access object's properties with array notation.
Consider using a for...in loop

Categories

Resources