简体   繁体   English

异步查询JSON对象

[英]Asynchronous Query JSON object

I have been playing around with a module from NPM called JSON-Query, I originally able to make the module function with JSON embedded in my js. 我一直在使用NPM的一个名为JSON-Query的模块,我最初能够通过嵌入js的JSON使该模块起作用。

I have spent about two days attempting to make it query JSON that is external and in a JSON file. 我花了大约两天的时间试图使其查询外部的JSON文件中的JSON。

The original code that was functioning looked something like this. 运行的原始代码看起来像这样。

var jsonQuery = require('json-query')
var data = {
  people: [
    {name: 'Matt', country: 'NZ'},
    {name: 'Pete', country: 'AU'},
    {name: 'Mikey', country: 'NZ'}
  ]
}

jsonQuery('people[country=NZ].name', {
  data: data
}) //=> {value: 'Matt', parents: [...], key: 0} ... etc 

I was able to query the internal JSON to find the key I was looking for. 我能够查询内部JSON来查找所需的密钥。

I realized I need the ability to update the JSON while the code is live, so I moved the JSON to its own file. 我意识到我需要在代码实时运行时能够更新JSON的能力,因此我将JSON移到了自己的文件中。

Currently my main JS file looks like this. 目前,我的主要JS文件如下所示。

var jsonQuery = require('json-query');
var fs = require('fs');

function querydb(netdomain){
fs.readFile('./querykeys.json', 'utf8', function (err, data) {
    if (err){console.log('error');} 
    var obj = JSON.parse(data);

    console.log(jsonQuery('servers[netshare=Dacie2015].netdomain', {
          obj: obj
        }));



});
}

querydb();

My JSON file that contains the json looks like this. 我的包含json的JSON文件如下所示。

{
          "servers": [
            {"netdomain": "google.com", "netshare": "password", "authip":"216.58.203.46"},
            {"netdomain": "localhost", "netshare": "localghost", "authip":"127.0.0.1"},
            {"netdomain": "facebook.com", "netshare": "timeline", "authip":"31.13.69.228"}
          ]
}

The issue I have ran into, I am unable to query the JSON anymore, when the function QueryDB() is ran, no matter what is in the place to query the JSON, i get no response locating my key. 我遇到的问题是,我再也无法查询JSON,而当函数QueryDB()运行时,无论查询JSON的位置如何,我都找不到定位键的响应。

Currently the response I get from the server when i try to query the JSON file is 目前,当我尝试查询JSON文件时,我从服务器获得的响应是

{ value: null,
  key: 'netdomain',
  references: [],
  parents: [ { key: 'servers', value: null }, { key: null, value: null } ] }

To be abundantly clear, i believe my issue is the way i call my object into play, i have played with the structure of the JSON-Query and have been unable to accomplish being able to isolate a key. 明确地说,我认为我的问题是调用对象的方式,使用JSON-Query的结构并且无法完成能够隔离键的问题。

Any help on this would be amazing, the module that i am working with can be found on npm at [NPM] https://www.npmjs.com/package/json-query 任何对此的帮助都将是惊人的,我正在使用的模块可以在npm的[NPM] https://www.npmjs.com/package/json-query上找到

Thank you 谢谢

I think this is just a typo. 我认为这只是一个错字。 Shouldn't this: 这不应该:

obj: obj

be this? 是这个吗

data: obj

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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