I am trying to process the data provided from: Twitch Helix (API) .
Use Javascript with: JSON.stringify
:
{"data":
[{
"id":"user_id",
"login":"user_login",
"display_name":"user_ds",
"type":"","broadcaster_type":"partner",
"description":"user_desc",
"profile_image_url":"url",
"offline_image_url":"url",
"view_count":123
},
{
"id":"user_id",
"login":"user_login",
"display_name":"user_ds",
"type":"","broadcaster_type":"partner",
"description":"user_desc",
"profile_image_url":"url",
"offline_image_url":"url",
"view_count":123
}
]
}
Then try to access by:
var datos = JSON.stringify(json);
console.log(datos.data);
But the result was: datos.data undefined
. I tried other ways but I'm already confused.
I also used PHP: json_decode() to see exactly how it was structured. Result:
array (
'data' =>
array (
0 =>
array (
'id' => 'user_id',
'login' => 'user_login',
'display_name' => 'user_ds',
'type' => '',
'broadcaster_type' => 'partner',
'description' => 'user_desc',
'profile_image_url' => 'url',
'offline_image_url' => 'url',
'view_count' => 123,
),
1 =>
array (
'id' => 'user_id',
'login' => 'user_login',
'display_name' => 'user_ds',
'type' => '',
'broadcaster_type' => 'partner',
'description' => 'user_desc',
'profile_image_url' => 'url',
'offline_image_url' => 'url',
'view_count' => 123,
),
),
)
Is there a possibility to access a certain field, eg: user_login
?
If possible, to access several user_login
, how can I do it with $.each()
?
Thanks.
You can do it in the following way
You declare within a variable the data you are trying to iterate, like this
Once the above is done, you are going to iterate over this same structure using a
forEach
, but first accessing the primary key that contains the rest of the values, which in this case is:data
remaining as followsCLARIFICATIONS
datos
key
main one, which in this case is datavalue
onekey
I do it throughkey["keyName"]
wherekey
is one of the arguments that I passed to said anonymous function inside theforEach
As a final result I get
Where, as you can see, when iterating through the values of
data
what is the primary key, when I place the key that I want to access, it shows me all the ones that match by name; in this case it is["view_count"]
To add the value returned by the key
["view_count"]
it would look like this