How calling right JSON for JavaScript [duplicate] - javascript

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);

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

find value in object based on multi level keys? [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 2 years ago.
Say I have an javascript object
i = {
data1:{
one:'555',
two:'222'
},
data2:{
}
}
I am suprised to see that there is no way to say i{data1}{one} to arrive at answer 555.. What is the most current way to get at this data without using a for loop?
You can easily arrive there with:
i.data1.one
or
i['data1']['one']

Get and output JSON [duplicate]

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 5 years ago.
I know this propably got answered very often but I still don't get it.
The only information I need is "up" and "down".
I got some JSON API Data and just want to get some specific data from it and just output it via alert().
What I got:
$.getJSON('URL', function(data) {
jsonData = data;
});
alert(data['items'][0]['up'][0]);
What the JSON looks like:
I tried to do this with the help of the mozilla wiki...

JSON - from regex to JSON [duplicate]

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.

Categories