OUJOOD.COM
Comment utiliser La méthode parseJSON() de jQuery
La méthode parseJSON() de jQuery prend une chaîne de caractères JSON correctement formée et renvoie la valeur JavaScript résultante.
Syntaxe :
jQuery.parseJSON( json )
Paramètres : La méthode parseXML() n'accepte qu'un seul paramètre qui est mentionné ci-dessus et décrit ci-dessous :
json : Ce paramètre est la chaîne JSON bien formée qui doit être analysée.
Valeur de retour : La méthode retourne les éléments suivants :
Chaîne de caractères
Nombre
Objet
Tableau
Booléen
Exemple 1 Dans cet exemple, nous passons une chaîne JSON bien formée à la méthode parseJSON().
Code
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>jQuery AJAX la méthode .parseJSON()</title>
<script src=" https://code.jquery.com/jquery-3.6.0.min.js "></script>
<!-- Script utilisant la méthode ajax -->
<style>
#a {
color: blue;
}
#b {
color: red;
}
</style>
</head>
<body>
<h1 style="color:green">www.oujood.com</h1>
<h2>jQuery la méthode ajax .parseJSON()</h2>
<h3>Laiser ajax faire le travail</h3>
<!--******************************************-->
<div>
<h3>JQuery la méthode parseJSON()</h3>
<b id="a"></b>
<script>
var json = '{"nom":"John", "Age":35, "Ville":"New York"}'
var obj = $.parseJSON(json);
document.getElementById("a").innerHTML =
"Object Nom : "+obj.nom + "<br> Object Age : " + obj.Age+" ans"+ "<br> Habite à: "+obj.Ville;
</script>
</div>
</body>
</html>
Exemple 2 :Dans cet exemple, nous passons une chaîne JSON bien formée à la méthode parseJSON(). Nous devons cliquer sur le bouton donné pour obtenir la sortie.
Code
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>jQuery AJAX la méthode .parseJSON()</title>
<script src=" https://code.jquery.com/jquery-3.6.0.min.js "></script>
<!-- Script utilisant la méthode ajax -->
<style>
#a {
color: blue;
}
#b {
color: red;
}
</style>
</head>
<body>
<h1 style="color:green">www.oujood.com</h1>
<h2>jQuery la méthode ajax .parseJSON()</h2>
<h3>Laiser ajax faire le travail</h3>
<!--******************************************-->
<div>
<h3>JQuery la méthode parseJSON()</h3>
<p><b id="a"></b></p>
<button onclick="gfg()">Click</button>
<script>
function gfg(){
var JSON = '{"nom":"Jaque du bois", "Age":21, "Voiture":"renault" }'
var obj = jQuery.parseJSON(JSON);
document.getElementById("a").innerHTML =
"Object Nom : "+obj.nom + "<br> Object Age : " + obj.Age+" ans"+ "<br> Voiture: "+obj.Voiture;
}
</script>
</div>
</body>
</html>