【问题标题】:parse to JSON in node.js在 node.js 中解析为 JSON
【发布时间】:2012-08-08 09:27:04
【问题描述】:

我有这个 Java 代码

DefaultHttpClient httpclient = new DefaultHttpClient();         
        HttpPost httpPostRequest = new HttpPost(URL);           
        StringEntity se;            
        se = new StringEntity(jsonObjSend.toString());          
        // Set HTTP parameters          
        httpPostRequest.setEntity(se);          
        httpPostRequest.setHeader("Accept", "application/json");            
        httpPostRequest.setHeader("Content-type", "application/json");          
        //httpPostRequest.setHeader("Accept-Encoding", "gzip"); 
        // only set this parameter if you would like to use gzip compression            
        long t = System.currentTimeMillis();            
        HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest); 

这在 node.js 中

var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
console.log("Entering");


if ( request.method === 'POST' ) {

     // the body of the POST is JSON payload.
     request.pipe(process.stdout);   
     }

});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");

我正在使用管道在控制台中写入所有内容,以确保我收到数据。 我真正想要的是将数据解析回 JSON,然后将其保存在数组中。 如何从请求中获取数据? 有人有代码示例吗?

谢谢

【问题讨论】:

    标签: android json http node.js post


    【解决方案1】:
    var http = require('http');
    // Configure our HTTP server to respond with Hello World to all requests.
    var server = http.createServer(function (request, response) {
    console.log("Entering");
    
    
    if ( request.method === 'POST' ) {
    
            // the body of the POST is JSON payload.
            request.pipe(process.stdout);   
    
            var data = '';
            request.on('data', function(chunk) {
                data += chunk;
            });
    
            request.on('end', function() {
                try {
                    data = JSON.parse(data);
                } catch (e) {
                    console.log(e);
                }
            });
        }
    
    });
    
    // Listen on port 8000, IP defaults to 127.0.0.1
    server.listen(8000);
    
    // Put a friendly message on the terminal
    console.log("Server running at http://127.0.0.1:8000/");
    

    【讨论】:

    • 非常感谢..我对“request.on('data', function(chunk)....”行有最后一个问题。参数'data'是什么意思...它是在某个地方定义的吗?
    • 'data' 和 'end' 是事件代码。检查此文档nodejs.org/api/http.html#http_event_data
    【解决方案2】:

    尝试在您的代码中使用以下概念

            response.on('data', function (chunk)
            {
                    var data = chunk.toString();
                    var data_val = JSON.parse(data)
              });
    

    【讨论】:

      猜你喜欢
      • 2013-02-15
      • 2017-04-06
      • 2014-12-11
      • 1970-01-01
      • 2011-09-23
      • 2015-01-07
      • 2011-05-28
      • 2020-12-15
      • 1970-01-01
      相关资源
      最近更新 更多