JSON - from regex to JSON [duplicate] - javascript

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
how do you get objects for events -> performances -> name & occurs_at to this json file https://api.myjson.com/bins/w05x using javascript. Cannot use regex since json API keeps changing format. Thanks

Use the JSON parser
obj = JSON.parse(json_string);
then you access it with
obj.events[event_index].performances[performance_index].performer.name
obj.events[event_index].name
obj.events[event_index].occurs_at
JSFiddle example of listing all the event attributes name and occurs_at.

Related

How do I convert a string into a property in javascript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 1 year ago.
Now I am sending length as a string as a function parameter.
function sample(operation){
var str="hello";
console.log(str.operation);
}
sample("length");
I am not allowed to change the way I am sending length(has to be a string).What can I do for this function to give me the expected output?
If you just want to access the property, use bracket notation for your property accessor:
console.log(str[operation])

Read from JSON where the name is [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 1 year ago.
My resultjson's content:
[{"name":"Dummy","id":"780828872962080819","sync_code":123456,"expiration":"2021-04-07T14:03:34.000Z"}]
I need the "name" from the JSON as a String, i tried with JSON.parse(resultjson).name but it didnt work.
It's an array. You need to access an element:
JSON.parse(resultjson)[0 /* or other element */].name

How calling right JSON for JavaScript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
I have a problem with calling the appropriate parameters for my function from this DOM:
data
weather:Array(1)
0: {id:800, main:"Clouds", description: "few clouds", icon: "02n"}
I write this (but it doesn'work):
$(".myClass").html(data.weather:Array(1).0.description);
How I must write this code to get description?
You were not accessing array properly. Try it.
$(".myClass").html(data.weather[0].description);

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