Given the response of a JSON file:
{
"Response": [
{
"ResultCode": "000",
"ResultMessage": "OK",
"NRecord": "2",
"Details": [
{
"nomepacchetto": "Nome Pacchetto",
"tipotraffico": "C",
"numerominuti": "100.00000",
"importocanone": "9.99",
"loginultimamodifica": "claudia.mentuccia",
"dataorainiziovalidita": "2022-04-28 14:10:01",
"loginapprovazione": "",
"dataapprovazione": "0000-00-00 00:00:00",
"id": "4"
},
{
"nomepacchetto": "Nome Pacchetto_2",
"tipotraffico": "C",
"numerominuti": "10.00000",
"importocanone": "5.00",
"loginultimamodifica": "claudia.mentuccia",
"dataorainiziovalidita": "2022-04-28 14:11:29",
"loginapprovazione": "",
"dataapprovazione": "0000-00-00 00:00:00",
"id": "5"
}
]
}
]
}
How can I retrieve all identifying data by id through JavaScript?
function StampaForm() {
var TblContenuto;
var TblContenuto = JSON.parse(sessionStorage.wbc_gestionepacchetti800_applicazione);
var form = "<form action=\"wbc_gestionepacchetti800_3.html\" name='dati' method=\"post\"><div class='tabella_select'><tbody class='body_form'><table class='table table-responsive-sm'>";
var output = '';
var output = '<tr>' '<td >' '<p>Nome pacchetto\n</p>' ' <input type=text id="floatingInput" name=nuovoip size=40 value="' TblContenuto[i].nomepacchetto '">' '</td>' '<td></td>' '<tr>';
$("#mod").append(form output);
}
The variable var TblContenuto = JSON.parse(sessionStorage.wbc_gestionepacchetti800_applicazione); alert(idupdate);
shows all the contents of my JSON file (above), what I would like to do is create a function that extrapolates the data fragment based on the ID, example:
id = 5:
{
"nomepacchetto": "Nome Pacchetto_2",
"tipotraffico": "C",
"numerominuti": "10.00000",
"importocanone": "5.00",
"loginultimamodifica": "claudia.mentuccia",
"dataorainiziovalidita": "2022-04-28 14:11:29",
"loginapprovazione": "",
"dataapprovazione": "0000-00-00 00:00:00",
"id": "5"
}
CodePudding user response:
You can also do this (avoid to add all element one by one) and gives you the JSON (the code is below the json variable, used for test) :
var json='{ \
"Response": [ \
{ \
"ResultCode": "000", \
"ResultMessage": "OK", \
"NRecord": "2", \
"Details": [ \
{ \
"nomepacchetto": "Nome Pacchetto", \
"tipotraffico": "C", \
"numerominuti": "100.00000", \
"importocanone": "9.99", \
"loginultimamodifica": "claudia.mentuccia", \
"dataorainiziovalidita": "2022-04-28 14:10:01", \
"loginapprovazione": "", \
"dataapprovazione": "0000-00-00 00:00:00", \
"id": "4" \
}, \
{ \
"nomepacchetto": "Nome Pacchetto_2", \
"tipotraffico": "C", \
"numerominuti": "10.00000", \
"importocanone": "5.00", \
"loginultimamodifica": "claudia.mentuccia", \
"dataorainiziovalidita": "2022-04-28 14:11:29", \
"loginapprovazione": "", \
"dataapprovazione": "0000-00-00 00:00:00", \
"id": "5" \
} \
] \
} \
] \
}';
test=JSON.parse(json);
response=test.Response;
response[0].Details.forEach(function(element ) {
if (element["id"]=="5") { //Change this value to get the id you want
$("#result").html(JSON.stringify(element)); //Remove function to have the variable (element)
$.each(element, function(key, value) {
$("#calc").append("Key :",key," Value:",value,"<br/>");
});
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="result">
</div>
<div id="calc">
</div>