Cara Mendeteksi JSON Valid atau Tidak menggunakan JQuery

JSON atau JavaScript Object Notation merupakan format teks yang didalamnya terkandung variable berbentuk larik (array) semua isinya terdiri dari pasangan kunci dan nilai (Key Value Pair).
132  
       

JSON atau JavaScript Object Notation merupakan format teks yang didalamnya terkandung variable berbentuk larik (array) semua isinya terdiri dari pasangan kunci dan nilai (Key Value Pair). Namun, tidak jarang format ini sering invalid atau tidak sah padahal formatnya sudah benar.

Untuk memeriksanya, maka dapat menggunakan kode.


var response=jQuery.parseJSON('response from server');
if(typeof response =='object')
{
// It is JSON
}
else
{
if(response ===false)
{
// the response was a string "false", parseJSON will convert it to boolean false
}
else
{
// the response was something else
}
}

Demikian.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>