无法通过索引解析Json

时间:2018-07-08 10:05:17

标签: javascript arrays json node.js

我正在尝试解析JSON文件,但遇到一些问题

{"prices": {"XRP/ETH": "0.0011228", "LTC/XRP": "187.7162", "BCH/INR": "53200.000", "LTC/BCH": "0.0729828", "LTC/BTC": "0.01567781", "LTC/ETH": "0.1948464", "XRP/BTC": "0.00006908", "BCH/ETH": "1.6930769", "XRP/INR": "32.000", "XRP/BCH": "0.0003879", "BCH/BTC": "0.22700587", "ETH/INR": "30500.000", "LTC/INR": "5280.000", "BTC/INR": "453000.000", "ETH/BTC": "0.07717977"}, "market": {"XRP/ETH": {"top_ask": "0.0010922 ETH", "top_bid": "0.0009030 ETH"}, "LTC/XRP": {"top_ask": "193.2886 XRP", "top_bid": "165.0000 XRP"}, "BCH/INR": {"top_ask": "56000.000 INR", "top_bid": "46100.000 INR"}, "LTC/BCH": {"top_ask": "0.1249458 BCH", "top_bid": "0.0942857 BCH"}, "LTC/BTC": {"top_ask": "0.01280000 BTC", "top_bid": "0.01147826 BTC"}, "LTC/ETH": {"top_ask": "0.1965870 ETH", "top_bid": "0.1600000 ETH"}, "XRP/BTC": {"top_ask": "0.00007111 BTC", "top_bid": "0.00006478 BTC"}, "BCH/ETH": {"top_ask": "1.9112628 ETH", "top_bid": "1.3969697 ETH"}, "XRP/INR": {"top_ask": "32.000 INR", "top_bid": "29.800 INR"}, "XRP/BCH": {"top_ask": "0.0006941 BCH", "top_bid": "0.0005321 BCH"}, "BCH/BTC": {"top_ask": "0.12444444 BTC", "top_bid": "0.10021739 BTC"}, "ETH/INR": {"top_ask": "33000.000 INR", "top_bid": "29300.000 INR"}, "LTC/INR": {"top_ask": "5760.000 INR", "top_bid": "5280.000 INR"}, "BTC/INR": {"top_ask": "460000.000 INR", "top_bid": "450000.000 INR"}, "ETH/BTC": {"top_ask": "0.07333333 BTC", "top_bid": "0.06369565 BTC"}}}

我正在尝试用Javascript解析它,但是收到一个错误的未定义对象,

我的javascript代码:-

request(ticker, function (err, body) {
    var a = JSON.parse(body.body);
    var p= a.prices;
    console.log(p[0]);
    console.log(p.length);

});

我希望console.log在JSON中打印出“ XRP / ETH”的第一个迭代,我也想找到数组的长度,但是收到未定义的错误

3 个答案:

答案 0 :(得分:1)

因为a.prices不是数组(将通过数字键索引),而是对象

{ "prices": {"XRP/ETH": "0.0011228", ... } }
            ^                            ^

注意括号({ })。

因此,a.prices[0]是无效的,但是a.prices["XRP/ETH"]是正确的:

var a = {"prices": {"XRP/ETH": "0.0011228" }}
console.log(a.prices["XRP/ETH"]);

要将其用作数组,可以使用Object.entries(obj)函数,该函数返回成对的数组,其中包含输入对象的键和值:

var a = {"prices": {"XRP/ETH": "0.0011228" }}
var p = Object.entries(a.prices);
console.log('Array:', p);
console.log('[0] key', p[0][0]);
console.log('[0] value', p[0][1]);
console.log('Length:', p.length);

答案 1 :(得分:0)

正如juzraai所述,您无法通过索引(structure [0])访问对象,需要通过键(structure [“ key”])访问对象,但是可以迭代访问对象的内容。从key:value角度观察对象,并且只记录第一次迭代,就像这样;

    request(ticker, function (err, body) {
    var a = JSON.parse(body.body);
    var p= a.prices;
    var count = 0;
    for (var k in p){
       //Ensure key exists in dict
       if (p.hasOwnProperty(k)) {
        if(count < 1){
        alert("Key is " + k + ", value is" + target[k]);

    }
    count++;        
       }
    }   
    console.log(count);
});

答案 2 :(得分:0)

一些观察结果:

  • 您的JSON已经是一个对象。因此,无需再次解析。
  • prices是一个对象,而不是数组。因此,应改为p[0]而不是p["XRP/ETH"]
  • 由于p不是数组,因此p.length将不起作用。相反,您可以尝试Object.keys(p).length

演示

var obj = {"prices": {"XRP/ETH": "0.0011228", "LTC/XRP": "187.7162", "BCH/INR": "53200.000", "LTC/BCH": "0.0729828", "LTC/BTC": "0.01567781", "LTC/ETH": "0.1948464", "XRP/BTC": "0.00006908", "BCH/ETH": "1.6930769", "XRP/INR": "32.000", "XRP/BCH": "0.0003879", "BCH/BTC": "0.22700587", "ETH/INR": "30500.000", "LTC/INR": "5280.000", "BTC/INR": "453000.000", "ETH/BTC": "0.07717977"}, "market": {"XRP/ETH": {"top_ask": "0.0010922 ETH", "top_bid": "0.0009030 ETH"}, "LTC/XRP": {"top_ask": "193.2886 XRP", "top_bid": "165.0000 XRP"}, "BCH/INR": {"top_ask": "56000.000 INR", "top_bid": "46100.000 INR"}, "LTC/BCH": {"top_ask": "0.1249458 BCH", "top_bid": "0.0942857 BCH"}, "LTC/BTC": {"top_ask": "0.01280000 BTC", "top_bid": "0.01147826 BTC"}, "LTC/ETH": {"top_ask": "0.1965870 ETH", "top_bid": "0.1600000 ETH"}, "XRP/BTC": {"top_ask": "0.00007111 BTC", "top_bid": "0.00006478 BTC"}, "BCH/ETH": {"top_ask": "1.9112628 ETH", "top_bid": "1.3969697 ETH"}, "XRP/INR": {"top_ask": "32.000 INR", "top_bid": "29.800 INR"}, "XRP/BCH": {"top_ask": "0.0006941 BCH", "top_bid": "0.0005321 BCH"}, "BCH/BTC": {"top_ask": "0.12444444 BTC", "top_bid": "0.10021739 BTC"}, "ETH/INR": {"top_ask": "33000.000 INR", "top_bid": "29300.000 INR"}, "LTC/INR": {"top_ask": "5760.000 INR", "top_bid": "5280.000 INR"}, "BTC/INR": {"top_ask": "460000.000 INR", "top_bid": "450000.000 INR"}, "ETH/BTC": {"top_ask": "0.07333333 BTC", "top_bid": "0.06369565 BTC"}}};

var p = obj.prices;

console.log(p["XRP/ETH"]);
console.log(Object.keys(p).length);