本文介绍了在创建节点JS JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要写在节点JS JSON字符串我的服务器上创建将被发送到客户端时,这一请求的。的问题是,此JSON取决于在服务器中的可用数据,从而,JSON数组的大小不总是相同的。从来就试图整整一天,但尽管我觉得是接近我仍然不明白这一点。

I need to create in my server written in node js a JSON string to be sent to the client when this request it. The problem is that this JSON depends on the available data in the server, thus, the size of the JSON array is not the same always. I´ve trying whole day but although I feel to be close I still dont get it.

这是示例查询如下:

json={"players":[
            {"name":"Messi", "goals":8},
            {"name":"Ronaldo", "goals":22},
            {"name":"Costa", "goals":20},
            {"name":"Neymar", "goals":13},
            {"name":"Arabi", "goals":6},
            {"name":"Bale", "goals":3},
            {"name":"Toquero", "goals":0}]};

我会通过将其发送到服务器

I would send it to the server by:

res.contentType('application/json');
res.send(json);

JSON数组,我想创建它依赖于一个名为目标散列一个球员的名字是关键,许多目标的价值。因此,如果只有3名球员,JSON数组只应该有这样的大小。

The JSON array that I want create it depends on a hash called 'goals' where a player name is the key and a number of goals the value. Thus, if there are only 3 players, the JSON Array only should have this size.

从来就一直在努力创建JSON数组网上是这样的:

I´ve been trying to create the JSON array online like this:

result= "";
for(i in goals){
    result = result+ '{ name:' + i + ", goals:" + goals[i] + '},';
}
result= result.substring(0, result.length - 1);
res.contentType('application/json');
res.send( { 'players': [ result]});

但是,客户端只接收大小为1的JSON

However, the client only receives a json of size 1

Object {jugadores: Array[1]}

jugadores:数组[1]
0:{农布雷:梅西,goles:8},{农布雷:罗纳尔多,goles:16},{农布雷:哥斯达黎加,goles:10},{农布雷:托克罗,goles:0},{农布雷:阿拉比,goles: 2},{农布雷:罢了,goles:10},{农布雷:内马尔,goles:8}
长度:1

jugadores: Array[1]0: "{ nombre:Messi, goles:8},{ nombre:Ronaldo, goles:16},{ nombre:Costa, goles:10},{ nombre:Toquero, goles:0},{ nombre:Arabi, goles:2},{ nombre:Bale, goles:10},{ nombre:Neymar, goles:8}"length: 1

在此先感谢,林真的与此挣扎:(

Thanks in advance, Im really struggling with this :(

编辑:林现在弦做这个尝试,但没有运气。我该怎么办错了?

Im trying now with stringy doing this, but no luck. What do I do wrong?

result= "players:[";
for(i in goals){
    result= result+ '{ name:' + i + ", goals:" + goals[i] + '},';
}

result= result.substring(0, resultado.length - 1);
result= result + ']'

res.contentType('application/json');
myJSONstring = JSON.stringify(resultado);
res.send(myJSONstring);

目标散列使用GET填充:

Goals hash is filled using GET:

app.get('/player/:id', function (req, res) {
res.contentType('application/json');
res.send( {'goals': + goals[req.params.id] } );

});

推荐答案

树立一个JavaScript数据结构与所需信息,然后把它变成末JSON字符串。

Build up a JavaScript data structure with the required information, then turn it into the json string at the end.

根据什么,我觉得你在做什么,尝试这样的:

Based on what I think you're doing, try something like this:

var result = [];
for (var name in goals) {
  if (goals.hasOwnProperty(name)) {
    result.push({name: name, goals: goals[name]});
  }
}

res.contentType('application/json');
res.send(JSON.stringify(result));

或类似的规定。

这篇关于在创建节点JS JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 09:37