在jQuery中迭代对象并输出到json数组

时间:2017-03-11 09:23:46

标签: jquery json

我正在尝试遍历对象列表,然后输出到JSON数组。我无法弄清楚如何破解这个。我正在使用带有节点的cheerio来抓取数据,如果有帮助的话。

我目前的输出是:

[
    {
        "gold_bid": "1204.50",
        "gold_ask": "1205.50",
        "gold_change": "+3.70",
        "gold_percent": "+0.31%",
        "silver": "",
        "palladium": "",
        "platinum": ""
    },
    {
        "gold_bid": "1204.50",
        "gold_ask": "1205.50",
        "gold_change": "+3.70",
        "gold_percent": "+0.31%",
        "silver": "",
        "palladium": "",
        "platinum": ""
    },
    {
        "gold_bid": "1204.50",
        "gold_ask": "1205.50",
        "gold_change": "+3.70",
        "gold_percent": "+0.31%",
        "silver": "",
        "palladium": "",
        "platinum": ""
    }
]

我希望实现的输出:

[
    {
        "gold_bid": "1204.50",
        "gold_ask": "1205.50",
        "gold_change": "+3.70",
        "gold_percent": "+0.31%"
    },
    {
        "silver_bid": "704.50",
        "silver_ask": "805.50",
        "silver_change": "+1.70",
        "silver_percent": "+0.21%"
    },
    {
        "palladium_bid": "204.04",
        "palladium_ask": "555.30",
        "palladium_change": "+1.10",
        "palladium_percent": "+1.05%"
    }
]

jQuery的:

var $ = cheerio.load(html);

      var gold_bid, gold_ask, gold_change, gold_percent, silver, palladium, platinum;
      var result = [];
      var json = { gold_bid : "", gold_ask: "",  gold_change: "", gold_percent: "",  silver : "", palladium : "", platinum: ""};


      $('.spot_price').filter(function(){
        var data = $(this);
        gold_bid = data.find('tr').eq(1).find('td').eq(3).text()
        gold_ask = data.find('tr').eq(1).find('td').eq(4).text()
        gold_change = data.find('tr').eq(1).find('td').eq(5).find('p').text()
        gold_percent = data.find('tr').eq(1).find('td').eq(6).find('p').text()

        json.gold_bid = gold_bid;
        json.gold_ask = gold_ask;
        json.gold_change = gold_change;
        json.gold_percent = gold_percent;

        silver_bid = data.find('tr').eq(2).find('td').eq(3).text()
        silver_ask = data.find('tr').eq(2).find('td').eq(4).text()
        silver_change = data.find('tr').eq(2).find('td').eq(5).find('p').text()
        silver_percent = data.find('tr').eq(2).find('td').eq(6).find('p').text()

        json.silver_bid = silver_bid;
        json.silver_ask = silver_ask;
        json.silver_change = silver_change;
        json.silver_percent = silver_percent;


      })

      for (var i = 1; i < 4; i++) {
         result.push(json);
      }

    }

    fs.writeFile('data.json', JSON.stringify(result, null, 4), function(err){
    })

任何帮助都会很扎实,这让我很精神。

*更新我想要实现的输出不会有相同的值...银色出价,询问,更改将是不同的值......

2 个答案:

答案 0 :(得分:0)

我建议这样的事情:

$('.spot_price').each(function(index, element) {
  if (gold) { // use your condition here
    var json = { gold_bid : "", gold_ask: "", gold_change: "", gold_percent: ""};
    gold_bid = data.find('tr').eq(1).find('td').eq(3).text();
    gold_ask = data.find('tr').eq(1).find('td').eq(4).text();
    gold_change = data.find('tr').eq(1).find('td').eq(5).find('p').text();
    gold_percent = data.find('tr').eq(1).find('td').eq(6).find('p').text();
    json.gold_bid = gold_bid;
    json.gold_ask = gold_ask;
    json.gold_change = gold_change;
    json.gold_percent = gold_percent;
    result.push(json);
  } else if (silver) { // use your condition here
    ... 
  } else if (platinum) { // use your condition here
    ... 
  } 
});

代码假定.spot_price - DOM对象包含有关金,银或铂的信息(正好是其中之一)。

答案 1 :(得分:0)

var $ = cheerio.load(html);

var gold_bid, gold_ask, gold_change, gold_percent, silver, palladium, platinum;
var result = [];
var json = { gold : "", silver : "", palladium : "", platinum: ""};


$('.spot_price').filter(function(){
    var data = $(this);
    gold_bid = data.find('tr').eq(1).find('td').eq(3).text()
    gold_ask = data.find('tr').eq(1).find('td').eq(4).text()
    gold_change = data.find('tr').eq(1).find('td').eq(5).find('p').text()
    gold_percent = data.find('tr').eq(1).find('td').eq(6).find('p').text()

    $.each(json,function(k,v){
     var innerjson={}; 
      innerjson[k+"_bid"]=gold_bid;
      innerjson[k+"_ask"]=gold_ask;
      innerjson[k+"_change]=gold_change;
      innerjson[k+"_percent"]=gold_percent;
      result.push(innerjson);

    });

});

fs.writeFile('data.json', JSON.stringify(result, null, 4), function(err){})

我猜这将适合您的情况