To access the information in a tag in a list, how would I do so? Could I use dot notation? Javascript is being used in the backend and javascript is being used in the frontend.
HTML:
<div>{{ GameData }}</div>
JAVASCRIPT:
{
"player_stat_summary": [
{
"id": 1,
"stats": {
"id": 1,
"average_node_capture_assist": 0,
"max_node_neutralize_assist": 0,
"total_minion_kills": 0,
"max_champions_killed": 0,
"total_champion_kills": 0,
"average_champions_killed": 0,
"average_num_deaths": 0,
"max_node_capture": 0,
"max_objective_player_score": 0,
"total_neutral_minions_killed": 0,
"max_assists": 0,
"average_combat_player_score": 0,
"max_node_capture_assist": 0,
"average_objective_player_score": 0,
"max_team_objective": 0,
"total_assists": 0,
"average_node_capture": 0,
"average_total_player_score": 0,
"average_team_objective": 0,
"average_node_neutralize": 0,
"max_node_neutralize": 0,
"average_node_neutralize_assist": 0,
"average_assists": 0,
"max_total_player_score": 0,
"max_combat_player_score": 0,
"total_turrets_killed": 0,
"total_node_neutralize": 0,
"total_node_capture": 0
},
"player_stat_summary_type": "Coop",
"wins": 100,
"losses": 0
}
],
"time_stamp": "2015-05-22T15:54:43.069814Z",
"summoner_id": 0
}
You can use dot notation as well as string notation. For example, in the given data, you can access the first object of the data as
GameData.player_stat_summary[0]
OR
GameData["player_stat_summary"][0]
You can any property of "player_stat_summary" as:
GameData.player_stat_summary[0].id
OR
GameData["player_stat_summary"][0]["id"]
Similarly more hierarchy can be accessed.
Related
I've 2 json files of with identical key fields on both the file. I would like to get your assistance to do arithmetic subtraction of values of same key field between 2 files and present in 3rd output file [ delta of two json files to output json file].
Also 1st and 2nd json files has n of array of indicies so we need to do this in loop to map the difference values in 3rd json output file.
Example:
1st Json file: file1.json
[
{
"name": "Rock",
"pri": 21,
"size": 502173,
"gets": 0,
"searches": 40,
"search_time_ms": 25,
"fetches": 2,
"cache_mem_size": 0,
"cache_size": 0,
"cache_total": 2,
"hits": 0,
"misses": 2,
"index_total": 0,
"index_current": 0,
"merges_total": 0,
"refresh_total": 0
},
{
"name": "Rock:config",
"pri": 21,
"size": 512173,
"gets": 0,
"searches": 40,
"search_time_ms": 25,
"fetches": 2,
"cache_mem_size": 0,
"cache_size": 0,
"cache_total": 2,
"hits": 0,
"misses": 2,
"index_total": 0,
"index_curr": 0,
"merges_tot": 0,
"refresh_tot": 0
}
]
2nd Json file: file1.json:
[
{
"name": "Rock",
"pri": 22,
"size": 602173,
"gets": 0,
"searches": 40,
"search_time_ms": 25,
"fetches": 2,
"cache_mem_size": 0,
"cache_size": 0,
"cache_total": 2,
"hits": 0,
"misses": 2,
"index_total": 0,
"index_current": 0,
"merges_total": 0,
"refresh_total": 0
},
{
"name": "Rock:config",
"pri": 31,
"size": 602173,
"gets": 0,
"searches": 40,
"search_time_ms": 25,
"fetches": 2,
"cache_mem_size": 0,
"cache_size": 0,
"cache_total": 2,
"hits": 0,
"misses": 2,
"index_total": 0,
"index_curr": 0,
"merges_tot": 0,
"refresh_tot": 0
}
]
Output json file: file3.json should look like below.
[
{
"name": "Rock",
"pri": 1,
"size": 100000,
"gets": 0,
"searches": 40,
"search_time_ms": 25,
"fetches": 2,
"cache_mem_size": 0,
"cache_size": 0,
"cache_total": 2,
"hits": 0,
"misses": 2,
"index_total": 0,
"index_current": 0,
"merges_total": 0,
"refresh_total": 0
},
{
"name": "Rock:config",
"pri": 10,
"size": 90000,
"gets": 0,
"searches": 40,
"search_time_ms": 25,
"fetches": 2,
"cache_mem_size": 0,
"cache_size": 0,
"cache_total": 2,
"hits": 0,
"misses": 2,
"index_total": 0,
"index_curr": 0,
"merges_tot": 0,
"refresh_tot": 0
}
]
please help me with the logic
This can be done as simple as u follow the following steps
Read the json object from 2 json files and save them as list of dictionaries
Loop through the list of dicts and perform your required manipulations
Save calculated output dicts to a new list
Write the resultant list to a new json file
the code is as follows,
import json
with open('file1.json') as f:
data1 = json.load(f)
with open('file2.json') as f:
data2 = json.load(f)
data = []
for i,j in zip(data1, data2):
x = {}
for m,n in zip(i.items(), j.items()):
# if m[0] != "name":
if m[0] == "pri" or m[0] == "size":
x[m[0]] = int(j[m[0]]) - int(i[m[0]])
else:
x[m[0]] = i[m[0]]
data.append(x)
with open('file3.json', 'w') as o:
json.dump(data, o)
Is there a way to import some data from json file into html file.
In need to import some of these data to a html file,
for example "Descrizione" and "Listino1" in a simple div.
Following is my json file sample:
[
{
"OP": "save",
"DB_NAME": "plu",
"ID": "005045",
"Barcode": "",
"CodVeloce": 0,
"CodPv": 0,
"FlagCondiviso": 1,
"CodTaccuino": 0,
"Descrizione": "BICC. VINO BIANCO",
"Famiglia": "006",
"ScortaMinima": 0,
"Posizione": 181,
"Reparto": 2,
"CodImmagini": 0,
"Listino1":4.00,
"Listino2":4.00,
"Listino3":0.00,
"Listino4":0.00,
"Stampante": 0,
"Flag": 1,
"Offerta": 0,
"Colore": 19,
"UnitMis": "",
"CodFornitore": 0,
"CodArtFornitore": "",
"BarcodeConfezione": "",
"PezziXConfezione": 0,
"ForzaLavoro": 0,
"PesoUnitario":0.00,
"NumConfezioni": 0,
"DescrizAggiuntiva": "",
"OffertaAggiuntiva": 0,
"PrezzoConfezione":0.00,
"ColoreTac": 0,
"Scala": 0,
"DtVar": "2020/02/03"
},
Any help.
Thanks in advance
I am having some problems with migration from ThreeJS geometry format 3 to 4. More specifically, i would like to manually create a surface in model.json file, but i don't get it to work.
in geometry format 3, the following code creates a single surface:
{
"faces": [1, 0, 1, 2, 3],
"metadata": {
"faces": 1
},
"vertices": [0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0]
}
Can some-one help me how to convert this to format 4?
The only example on geometry format 4 i could find was this one: https://github.com/mrdoob/three.js/wiki/JSON-Geometry-format-4
This is the equivalent code in json-geometry format 4:
{
"vertices": [0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0],
"normals": [],
"uvs": [],
"faces": [ 1, 0, 1, 2, 3]
}
If I wanted to convert the stats in json to a readable list, how would I do that?
For example,
{
"player_stat_summary": [
{
"id": 1,
"stats": {
"id": 1,
"average_node_capture_assist": 0,
"max_node_neutralize_assist": 0,
"total_minion_kills": 0,
"max_champions_killed": 0,
"total_champion_kills": 0,
"average_champions_killed": 0,
"average_num_deaths": 0,
"max_node_capture": 0,
"max_objective_player_score": 0,
"total_neutral_minions_killed": 0,
"max_assists": 0,
"average_combat_player_score": 0,
"max_node_capture_assist": 0,
"average_objective_player_score": 0,
"max_team_objective": 0,
"total_assists": 0,
"average_node_capture": 0,
"average_total_player_score": 0,
"average_team_objective": 0,
"average_node_neutralize": 0,
"max_node_neutralize": 0,
"average_node_neutralize_assist": 0,
"average_assists": 0,
"max_total_player_score": 0,
"max_combat_player_score": 0,
"total_turrets_killed": 0,
"total_node_neutralize": 0,
"total_node_capture": 0
},
"player_stat_summary_type": "Coop",
"wins": 100,
"losses": 0
}
],
"time_stamp": "2015-05-22T15:54:43.069814Z",
"summoner_id": 0
}
You don't say what language you are using.
Assuming JS.
you can use JSON.parse(text)
so assuming that your JSON string is stored in a variable called text
var stats= JSON.parse(text);
console.log(stats.player_stat_summary[0].id);
> 1
However, it looks like that's not a string at all,but is actually already an object. You're just not assigning it to a variable. If this is the case, just assign it to a variable and use it as I did above, no need for JSON.parse(text);
e.g.
stats={....};
console.log(stats.player_stat_summary[0].id);
> 1
If you want to iterate over the stats and emit an HTML list you could do something like the following:
var stats = {
"player_stat_summary": [
{
"id": 1,
"stats": {
"id": 1,
"average_node_capture_assist": 0,
"max_node_neutralize_assist": 0,
"total_minion_kills": 0,
"max_champions_killed": 0,
"total_champion_kills": 0,
"average_champions_killed": 0,
"average_num_deaths": 0,
"max_node_capture": 0,
"max_objective_player_score": 0,
"total_neutral_minions_killed": 0,
"max_assists": 0,
"average_combat_player_score": 0,
"max_node_capture_assist": 0,
"average_objective_player_score": 0,
"max_team_objective": 0,
"total_assists": 0,
"average_node_capture": 0,
"average_total_player_score": 0,
"average_team_objective": 0,
"average_node_neutralize": 0,
"max_node_neutralize": 0,
"average_node_neutralize_assist": 0,
"average_assists": 0,
"max_total_player_score": 0,
"max_combat_player_score": 0,
"total_turrets_killed": 0,
"total_node_neutralize": 0,
"total_node_capture": 0
},
"player_stat_summary_type": "Coop",
"wins": 100,
"losses": 0
}
],
"time_stamp": "2015-05-22T15:54:43.069814Z",
"summoner_id": 0
}
function emitStats(s) {
// create a new UL
var list = document.createElement('ul');
// iterate over the properties
Object.keys(s).forEach(function(statName) {
// create an LI for the property
var el = document.createElement('li');
// fill it with the stat name and value
el.innerHTML = statName.replace(/_/g,' ') + ": " + s[statName];
// add it to the UL
list.appendChild(el);
});
// add the UL to the document.
document.getElementById('content').appendChild(list);
}
// loop over each entry in stats.player_stat_summary
for(var i = 0; i < stats.player_stat_summary.length; i++ ) {
// invoke emitStats, passing the entry's stats property.
emitStats(stats.player_stat_summary[i].stats);
}
<div id="content"></div>
I'm getting my JSON object from the RIOT games API. Here is part of the JSON I get back:
{
"modifyDate": 1431043986000,
"champions": [
{
"id": 110,
"stats": {
"totalDeathsPerSession": 9,
"totalSessionsPlayed": 1,
"totalDamageTaken": 16507,
"totalQuadraKills": 0,
"totalTripleKills": 0,
"totalMinionKills": 164,
"maxChampionsKilled": 2,
"totalDoubleKills": 0,
"totalPhysicalDamageDealt": 101747,
"totalChampionKills": 2,
"totalAssists": 2,
"mostChampionKillsPerSession": 2,
"totalDamageDealt": 105995,
"totalFirstBlood": 0,
"totalSessionsLost": 1,
"totalSessionsWon": 0,
"totalMagicDamageDealt": 4248,
"totalGoldEarned": 7932,
"totalPentaKills": 0,
"totalTurretsKilled": 0,
"mostSpellsCast": 0,
"maxNumDeaths": 9,
"totalUnrealKills": 0
}
},
{
"id": 113,
"stats": {
"totalDeathsPerSession": 6,
"totalSessionsPlayed": 1,
"totalDamageTaken": 39226,
"totalQuadraKills": 0,
"totalTripleKills": 0,
"totalMinionKills": 75,
"maxChampionsKilled": 5,
"totalDoubleKills": 1,
"totalPhysicalDamageDealt": 28711,
"totalChampionKills": 5,
"totalAssists": 8,
"mostChampionKillsPerSession": 5,
"totalDamageDealt": 177132,
"totalFirstBlood": 0,
"totalSessionsLost": 1,
"totalSessionsWon": 0,
"totalMagicDamageDealt": 120299,
"totalGoldEarned": 12544,
"totalPentaKills": 0,
"totalTurretsKilled": 1,
"mostSpellsCast": 0,
"maxNumDeaths": 6,
"totalUnrealKills": 0
}
},
and so on for all of the champions in the game (there are around 150). If I wanted to calculate the total win/loss ratio or percent, I would have to find the total of "totalSessionsLost" and "totalSessionsWon". How would I go about totaling all of the values for this?
Thanks
var total_win = 0,
total_lost = 0,
champions = yourjsonobj.champions;
for (var i = 0; i < champions.length; i++) {
total_win += champions[i].totalSessionsWon;
total_lost += champions[i].totalSessionsLost;
}