如何抓取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 (err) { console.error('Error sending response with code 500.'); } }; function replyNotFound(res) { res.writeHead(404); res.end('not found'); } function handleRequest(req, res) { console.log('Handling request for ' + req.url); if (req.url === '/') { root.handle(req, res); } if (req.url === '/image.png'){ image.handle(req, res); } else { replyNotFound(res); } } var server = http.createServer(); server.on('request', function(req, res) { var d = domain.create(); d.on('error', function(err) { console.error(req.url, err.message); replyError(res); }); d.run(function() { handleRequest(req, res)}); }); server.listen(5000); 

那么message.js或者root(在min var root = require(/ message)

 var http = require('http'); var body; exports.handle = function(req, res) { res.writeHead(200, { 'Content-Type': 'application/json', 'Content-Length': body.length }); res.end(body); }; exports.init = function(cb) { require('fs').readFile('app.html', function(err, data) { if (err) throw err; body = data; cb(); }); } 

app.html

  <html> <header><title>This is title</title></header> <body> Hello world <span id="ajaxButton" style="cursor: pointer; text-decoration: underline"> Make a request </span> <script type="text/javascript"> (function() { var httpRequest; document.getElementById("ajaxButton").onclick = function() { makeRequest('message.js'); }; function makeRequest(url) { if (window.XMLHttpRequest) { // Mozilla, Safari, ... httpRequest = new XMLHttpRequest(); } else if (window.ActiveXObject) { // IE try { httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!httpRequest) { alert('Giving up :( Cannot create an XMLHTTP instance'); return false; } httpRequest.onreadystatechange = alertContents; httpRequest.open('GET', url); httpRequest.send(msg); } function alertContents() { if (httpRequest.readyState === 4) { if (httpRequest.status === 200) { alert(httpRequest.responseText); } else { alert('There was a problem with the request.'); } } } })(); </script> </body> </html> 

尝试改变body = data;body = data.msg; 在你的message.js文件中。 这将查找JSON对象中的键msg的值,并将结果分配给bodyvariables。

编辑

alertContents函数中,replace:

 alert(httpRequest.responseText); 

有:

 var resData = JSON.parse(httpRequest.ResponseText); alert(resData.msg); 

这样做是将JSON响应parsing为一个Javascript对象,然后查找msg属性并将其用于警报。

编辑2

任何时候你需要parsingJSON,使用JSON.parse(jsonString)。 它将返回一个本地JavaScript对象,您可以将其存储在variables中并查找属性,如上例所示。

我看不到你从哪里收到msg的信息,但是在你接收JSONstring的地方就可以做到这一点。

 var jsonResult = receivedData(); if (jsonResult && jsonResult.msg) { alert(jsonResult.msg);} }