Tag: json

如何抓取JSONstring中的数据

大家好我试图在我的网页上显示"hello JSON" ,但它显示完整的string{ "msg": "Hello JSON." } { "msg": "Hello JSON." } 。 我知道为什么这样做,但我怎么能得到它只显示hello JSON而不只是把它放到一个HTML脚本,并使用它? 这是我的代码: var http = require('http'); var domain = require('domain'); var root = require('./message'); // do I have to replace root w/ message var image = require('./image'); // for better readability? function replyError(res) { try { res.writeHead(500); res.end('Server error.'); } catch […]

无法通过命令行安装Grunt

我正在尝试安装Grunt命令行,并且在安装时遇到困难。 我非常仔细地跟着克里斯·科伊尔的演练,看不出我哪里出错了。 我的项目名称是'ryan',这里是我的package.json: { "name": "ryan", "version": "0.1.0", "devDependencies": { "grunt": "~0.4.2" } } 作为node_modules被安装到项目文件夹中,Grunt似乎安装得很好。 但是,一旦我input'npm install -g grunt-cli',似乎就失败了。 任何解决scheme 我的terminal: ryans-mbp:ryan rynslmns$ npm install -g grunt-cli npm http GET https://registry.npmjs.org/grunt-cli npm http 304 https://registry.npmjs.org/grunt-cli npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules/grunt-cli' npm ERR! { [Error: EACCES, mkdir '/usr/local/lib/node_modules/grunt-cli'] npm ERR! errno: 3, npm ERR! code: […]

Mongo GeoJSON:预期位置对象

我试图做一个简单的GeoJSON对象插入到Mongo数据库的操作: > db.users.ensureIndex({'loc': '2d'}) > db.users.insert({'loc': {'type': 'Point', 'coordinates': [50, 50]}}) location object expected, location array not in correct format 没有外部的背景 – 这是一个新的数据库和蒙戈壳内的新鲜收集。 我究竟做错了什么?

parsingJed对象

我们有一个应用程序生成复杂的JSON对象,其属性和值已被转换为hex。 我怎样才能把它们还原成string? 示例对象: { "636f756e747279": { "6e616d65": "43616e616461", "636f6465": "4341" }, "617574686f72": { "6e616d65": "4a61736d696e652048656174686572", "67656e646572": "66", "626f6f6b496473": [ "6a65683233696f", "33393233393130" ] } } 目前代码: var data = require( './data.json' ); var parsed = {}; Object.keys( data ).forEach( function( key, index, keys ) { var prop = new Buffer( key, 'hex' ).toString(); var value = […]

对象JSON转换的问题

我有一个对象(jar),它包含这个(通过console.log): { _jar: { store: { idx: { localhost: { '/': { PHPSESSID: Cookie="PHPSESSID=pe1952pk023e7b6d7t9am3kse0; Path=/; hostOnly=true; aAge=18ms; cAge=97ms" } } } } } } 我试图将其存储到MongoDB实例,这是可以的,但从那里加载后,这是一种格式不正确。 这模拟它: console.log(JSON.parse(JSON.stringify(jar))); 以上输出结果如下: { _jar: { store: { idx: [Object] } } } 那么所有突如其来的“本地主机”部分都消失在“对象”中了? 如何防止这一点?

Nodejsasynchronous数据重复

我在nodejs上有一个asynchronous进程有问题。 我从远程JSON获取一些数据,并将其添加到我的数组中,这个JSON有一些重复的值,我需要检查它是否已经存在于我的数组之前,添加它,以避免数据重复。 我的问题是,当我启动JSON值之间的循环,循环调用下一个值,在最后一个过程完成之前,所以,我的数组充满重复的数据,而不是保持每个types只有一个项目。 看我目前的代码: BookRegistration.prototype.process_new_books_list = function(data, callback) { var i = 0, self = this; _.each(data, function(book) { i++; console.log('\n\n ———————————————————— \n\n'); console.log('BOOK: ' + book.volumeInfo.title); self.process_author(book, function() { console.log('in author'); }); console.log('\n\n ————————————————————'); if(i == data.length) callback(); }) } BookRegistration.prototype.process_author = function(book, callback) { if(book.volumeInfo.authors) { var author = { name: book.volumeInfo.authors[0].toLowerCase() }; […]

NodeJS – parsingJSON(仅string或数字)

我有一点困惑怎么办(我该怎么办)。 我已经在我的应用程序中有几个地方使用AJAX来传输JSON数据。 因此,例如从registry中接收数据: try { var data = JSON.parse(json); } catch(e) { // … } var fields = { firstName: data.firstName || "", lastName: data.lastName || "", … }; 那么我需要在这些领域做些什么。 我们说: if (fields.firstName) { // OK save it to the DB } if(fields.lastName.xxx()) { // xxx() is a method that belongs to JS String Object […]

JavaScript Express返回空的JSON

伙计们,我有一个函数创build一个对象,我想返回到浏览器。 出于某种原因,浏览器正在获取一个没有内容的空对象: { pName: [ ] } 这里是通过console.log(require('util').inspect(projectObject, false, 10));对象console.log(require('util').inspect(projectObject, false, 10)); { pName: [ foo: [ nodejs: [ staging: [ { name: 'i-4a14c51a', id: 'i-4a14c51a' }, { name: 'i-19fc094a', id: 'i-19fc094a' }, ], production: [ { name: 'i-5f14c50f', id: 'i-5f14c50f' }, { name: 'i-b1fb0ee2', id: 'i-b1fb0ee2' }, ] ] ], … 我会想象它不返回对象的原因是因为它认为第一个实体是空的。 我在哪里犯错误? 结果需要看起来喜欢: […]

Sails.js,获取请求params一个json对象

我在Sails.js中使用套接字 以下是一个示例客户端代码: socket.get('/message/send', {from: userId, to: recepientId, content: textMessage}); 这就是我如何获取控制器(服务器)中的数据: var to = req.param('to'); var from = req.param('from'); var content = req.param('message'); 现在,因为我发送的数据作为JSON,我想把它作为一个JSON对象。 所以消息最终是一个模型,我想直接使用json对象将数据存储在数据库中。 我使用socket.emit('channel',{userId: "someId"})时,类似的东西对我来说是有效的。所以我在socket.on('channel', function(userJsonObject){});接收到一个JSON对象socket.on('channel', function(userJsonObject){}); 那么当我使用socket.get()时,如何得到类似的JSON对象

通过websockets的Node.js JSON格式化问题

我有以下Node.js代码,调用天气web服务来获得json repsonse: var reqGet = https.request(optionsgetmsg, function(res) { console.log("statusCode: ", res.statusCode); // uncomment it for header details // console.log("headers: ", res.headers); res.on('data', function(d) { console.info('GET result after POST:\n'); process.stdout.write(d); console.info('\n\nCall completed'); }); return d; }); 当我使用process.stdout.write(d)到terminal的输出是相当JSON格式的文本,如下所示: { "response": { "version":"0.1", "termsofService":"http://www.wunderground.com/weather/api/d/terms.html", "features": { "geolookup": 1 , "conditions": 1 } } , "location": { "type":"CITY", "country":"US", […]