更新json对象中的json元素

时间:2017-12-15 01:25:10

标签: javascript json node.js

我想更新" Last" MarketName USDT-BTC的价格,如何修改" Last"从16750.00000001到17000.00000001并通过我的api发送?

{
  "success":true,
  "message":"",
  "result":[{
    "MarketName":"USDT-BTC",
    "High":16937,
    "Low":15280,
    "Volume":6268.37139646,
    "Last":16750.00000001,
    "BaseVolume":101115016.3188782,
    "TimeStamp":"2017-12-15T01:11:19.513",
    "Bid":16749.99999999,
    "Ask":16750,
    "OpenBuyOrders":12099,
    "OpenSellOrders":4901,
    "PrevDay":16143.70987342,
    "Created":"2015-12-11T06:31:40.633"
}]}
var express = require("express");
var app = express();
const request = require('request');

const options = {
    url: 'https://bittrex.com/api/v1.1/public/getmarketsummaries',
    method: 'GET',
};

app.get("/api", function(req, res)  {
        request(options, function(err, output, body) {
        var json = JSON.parse(body);
        delete json['USDT-BTC']; // THIS IS NOT WORKING WHAT DO I HAVE TO DO?
        console.log(json);
        res.json(json)
});

});

app.listen(80, function() {
    console.log("RUNNING: http://localhost/api");
});

module.exports = app;

2 个答案:

答案 0 :(得分:0)

您需要寻找USDT-BTC并更改价格:

-1
-2
-15
-31
31

使用map的其他方式:

 for (var i in json.result) {
     var item = json.result[i];

     if (item.MarketName == 'USDT-BTC') {
         item.Price = 17000.00000001;
     }
 }

答案 1 :(得分:0)

不确定我得到了你在做什么。你为什么要删除一些东西?对象键的拳头似乎是“Last”而不是“USDT-BTC”。因此,您需要让Jon对象找到正确的条目并退出“Last”键。像var entry = json [0]; entry ['Last'] =“new value”:

假设你在响应中只有1个东西和/或它是返回数组中的第一个元素。