简体   繁体   中英

Reading a JSON file in Javascript

first of all I have to say I've checked to other stackoverflow questions related to my problem but turns out none of them helped me. That's only why I post this.

I made this little phonegap/cordova app that works well :

index.html :

<html>
   <head>
      <title>NFC tag ID reader</title>
   </head>
   <body style="font-size: 1.4em;">
      <div class="app">
         <div id="messageDiv"></div>
      </div>
      <script type="text/javascript" src="cordova.js"></script>
      <script type="text/javascript" src="js/index.js"></script>

      <script type="text/javascript">
        app.initialize();
      </script>
   </body>
</html>

js/index.js :

var app = {
/*
   Application constructor
 */
   initialize: function() {
      this.bindEvents();
      console.log("Starting NFC Reader app");
   },
/*
   bind any events that are required on startup to listeners:
*/
   bindEvents: function() {
      document.addEventListener('deviceready', this.onDeviceReady, false);
   },

/*
   this runs when the device is ready for user interaction:
*/
   onDeviceReady: function() {

      nfc.addTagDiscoveredListener(
         app.onNfc,             // tag successfully scanned
         function (status) {    // listener successfully initialized
            app.display("Bonjour, veuillez vous identifier.");
         },
         function (error) {     // listener fails to initialize
            app.display("NFC reader failed to initialize " +
               JSON.stringify(error));
         }
      );
   },

/*
   displays tag ID from @nfcEvent in message div:
*/



   onNfc: function(nfcEvent) {
  var tag = nfcEvent.tag;
  var nfcUid = nfc.bytesToHexString(tag.id);
 var myDb = {
"048574220e2a80": {
    "name": "name1",
    "firstname": "fname1",
    "societe": "agency1"
},
"04ddfd12872a80": {
    "name": "name2",
    "firstname": "fname2",
    "societe": "agency2"
 },
"04d0fd12872a80": {
    "name": "name3",
    "firstname": "fname3",
    "societe": "agency3"
 }
 };


  app.display(myDb[nfcUid].name + ' ' + myDb[nfcUid].firstname + ' travaille chez : ' + myDb[nfcUid].societe);

  },

   /*
      appends @message to the message div:
   */
   display: function(message) {
      var label = document.createTextNode(message),
         lineBreak = document.createElement("br");
      messageDiv.appendChild(lineBreak);         // add a line break
      messageDiv.appendChild(label);             // add the text
   },
   /*
      clears the message div:
   */
   clear: function() {
       messageDiv.innerHTML = "";
   }
};     // end of app

What's interesting is the onNFC function that waits for a NFC tag and then display on the screen the name of the person corresponding the uid tag (by checking in the json database first). This way the datas are not stored in the tag, but directly in the json file.

What I would like is to use the same structure but with a json file out of the index.js file. And this is where I have problems...

My index.html does not change, I've just created a db.json file like this :

{
    "048574220e2a80": {
        "name": "name1",
        "firstname": "fname1",
        "societe": "agency1"
    },
    "04ddfd12872a80": {
        "name": "name2",
        "firstname": "fname2",
        "societe": "agency2"
     },
    "04d0fd12872a80": {
        "name": "name3",
        "firstname": "fname3",
        "societe": "agency3"
     }

What's changing in my /js/index.js file is the onNFC function but I don't know how to reach the db.json file and then doing the same thing as before. I tried several ways like using jquery or using JSON.parse but none of them works for me.

Any ideas guys ?

Thanks

I had the same problem. To read your file use JSON.parse(string) . It convert your string data to object .

I don't know much about cordova, and i am not really sure what you are trying to do exactly, but you can read files using cordova api as described here: http://cordova.apache.org/docs/en/2.5.0/cordova_file_file.md.html

If what you are reading is JSON, you can always pass it's content to JSON.parse function, which will return a native javascrip object. Be carefull that the json content is valid JSON or else JSON.parse will fail.

I don't fully understand your problem, but the following code and note may help you,

    var data = {
    "048574220e2a80": {
        "name": "name1",
        "firstname": "fname1",
        "societe": "agency1"
    },
    "04ddfd12872a80": {
        "name": "name2",
        "firstname": "fname2",
        "societe": "agency2"
     },
    "04d0fd12872a80": {
        "name": "name3",
        "firstname": "fname3",
        "societe": "agency3"
    }};
    alert(data['048574220e2a80']['name']);
    alert(data['048574220e2a80']['firstname']);
    alert(data['048574220e2a80']['societe']);

Is your data dynamically generated throw AJAX or static and known at the beginning?

If it is static you can put it in the js as static data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM