parse the object in javascript [duplicate] - javascript

This question already has answers here:
Parsing "relaxed" JSON without eval
(6 answers)
Convert JS object to JSON string
(23 answers)
Safely turning a JSON string into an object
(28 answers)
Closed 24 days ago.
var obj = '{name: "amit", age: 40}'
I have a object which im getting it in parse, how to parse this object ?

Try this -
var strObj = JSON.stringify(obj);
console.log(strObj);

Related

cannot print component in array in javascript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
How do I reference a JavaScript object property with a hyphen in it?
(11 answers)
Closed 2 months ago.
i'm just a newbie on javascript , please help me this
var arr = [{"student-name":"Harry Potter","discipline":"Magic"}] ;
how can i get "Harry Potter" in this array , thanks for helping

How to convert string object to object? [duplicate]

This question already has answers here:
Safely turning a JSON string into an object
(28 answers)
Closed 1 year ago.
I'm using typescript and I'm receiving a value with object format, but it's a string.
Lets call it as: myVar.
MyVar have this value (image below) as string.
const string myVar = '{"value":"1"}'
How can I transform this string to object for access your value?
you can use
JSON.parse('{"value": "1"}');
const myVar: string = '{"value":"1"}'
const myVarObj = JSON.parse(myVar)
console.log(myVarObj)
Playground Link

How can get the value of all the keys in json array at 1st json element using javascript/jquery [duplicate]

This question already has answers here:
Getting JavaScript object key list
(19 answers)
Closed 3 years ago.
var resultJSON = [{"name":"AAA","createdDate":"8/14/2019"},{"name":"ABAB","_id":"52f0e7719c3fccfabfaaf5218bad7d22","createdDate":"8/14/2019"},{"name":"BBB","_id":"848e3d2fcedad749fa8dc22b97db663a","createdDate":"8/14/2019"}];
for (var k in resultJSON[0]);
alert(resultJSON[k]);
Use Object.keys():
var resultJSON = [{"name":"AAA","createdDate":"8/14/2019"},{"name":"ABAB","_id":"52f0e7719c3fccfabfaaf5218bad7d22","createdDate":"8/14/2019"},{"name":"BBB","_id":"848e3d2fcedad749fa8dc22b97db663a","createdDate":"8/14/2019"}];
console.log(Object.keys(resultJSON[0]));

How to read JSON with multiple key value pair [duplicate]

This question already has answers here:
How to parse JSON data with jQuery / JavaScript?
(11 answers)
Closed 4 years ago.
On AJAX request response is following :
[ {"status":{"login":"invalid"}},
{"user":{"username":false,"pwd":false}}
]
How to read values in Jquery.
Use JSON.parse(string) to get an Object.
After that you can access values as default:
var json = JSON.parse(res.data)
console.log(json.status.login)

How to convert string to object in Angularjs [duplicate]

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 6 years ago.
I have a string like :
$scope.text = '"{\"firstName\":\"John\",\"age\":454 }"';
and I want to convert to js object:
$scope.tmp = {"firstName":"John","age":454 };
Note: JSON.parse() doesn't work!!
It's my sample in codepen
You can do it with angular.fromJson()
in your sample, it would have been $scope.tmp = angular.fromJson($scope.text);
The difference between JSON.Parse() and angular.fromJson, is that angular will check to make sure a string is provided. If it is already an object, it will return the same object.

Categories