Comment lire json en jquery?

Je ne suis pas familier avec jquery. Voulez-vous m'aider à cela? J'ai une réponse de json de url mais je ne sais pas comment, puis-je lire la valeur de clé dans jquery.

Par exemple, comment obtenir la valeur "HAWBItemEntity"?

Veuillez vérifier la réponse json ci-dessous.

{ "waybill_log": { "TrackingResult": { "HAWBEntity": { "HAWBID": 282829899, }, "HAWBHistoryEntity": [ { "ActionDate": "4/26/2014 12:32:00 PM", }, { "ActionDate": "4/26/2014 12:32:00 PM", } ], "HAWBAttachmentEntity": [ { "FileName": "Invoice_30018018516..pdf", } ], "HAWBItemEntity": null, }, "HAWBAttachmentEntityExtendedList": [ { "HAWBAttachmentEntity": { "FileName": "Invoice_30018018516..pdf", }, "AttachmentLink": "nw" } ], "CurrentStatus": "Delivery", "ConsolsData": { "ConsolNumber": null, }, "ItemContainerData": { "ContainerNumber": null, }, "FlightDetails": null, } } 

  1. Utilisez la méthode jQuery.parseJSON() de jQuery pour obtenir un objet JavaScript hors de votre JSON-String:

     var test = jQuery.parseJSON(data); // Where 'data' is your JSON string 
  2. Après l'analyse, le test est un objet JavaScript. Le document jQuery à propos de parseJSON() :

JQuery.parseJSON ()

Permet une chaîne JSON bien formée et renvoie l'objet JavaScript résultant.

À propos de l'objet Javascript:

 // Declaration var Obj = { // Properties: propertyOne: 'value', // string propertyTwo: 52.3654, // float // propertyThree is an object inside 'Obj' // defined by the braces // which may naturally contain its own properties & methods propertyThree: { propTrheeProperty: 42, // int propTrheeAnotherProperty: 'whatever', thePropThreeMethod: function () { // your function code } // and so on, no coma after the last property/method }, // and so on // 'Obj' - Methods: methodOne: function () { // your function code }, methodTwo: function () { // your function code } // and so on, no coma after the last property/method } 

Il existe deux possibilités d'accéder aux propriétés (mais pas aux méthodes, voir ci-dessous), appelé Accessoire de propriété :

– La "Notation de points":

Avec la notation par points, vous pouvez accéder aux propriétés et aux méthodes

 var objOne = new Obj(); // Create a new instance of Obj objOne.propertyTwo; // 52.3654 var objTwo = new Obj(); // Another instance of Obj objTwo.propertyThtree.propTrheeProperty; // 42 objTwo.propertyThtree.propTrheeAnotherProperty; // whatever // Accessing methods objOne.methodOne(); // whatever your function methodOne() returns or does objTwo.methodTwo(); // whatever your function methodTwo() returns or does 

– La "Notation de support":

Avec la notation de bracket, vous pouvez également accéder aux propriétés et aux méthodes

 objTwo['propertyThtree']['propTrheeProperty']; // 42 objOne['methodOne'](); 

au lieu de

 objTwo.propertyThtree.propTrheeProperty; // 42 objOne.methodOne(); 

Dans votre cas, cela signifie:

 window.console.log(test.waybill_log.TrackingResult.HAWBEntity.HAWBID); // 282829899 

Ou

 window.console.log(test.waybill_log.TrackingResult.HAWBEntity); // Should give something like: Object { HAWBID: '282829899'} 

Ou

 window.console.log(test.waybill_log.HAWBItemEntity); // null 

Vous n'avez pas à lire json avec jquery en général.

Il suffit de le lire avec facilité, en utilisant la fonction JSON.parse() et sans Jquery.

 var json = '{"result":true,"count":1}', obj = JSON.parse(json); alert(obj.count); 

Si vous utilisez jQuery pour obtenir le JSON, vous pouvez simplement utiliser:

http://api.jquery.com/jquery.getjson/

Et il analysera le JSON pour vous.

 var json = $.parseJson(jsonString); 

Pour obtenir la valeur 282829899 pour "HAWBID", vous utiliserez:

 var hawbid = json.waybill_log.TrackingResult.HAWBEntity.HAWBID;