5

好的,我一直在通过这个网站搜索任何类似或有启发性的东西,但我完全被卡住了。我得到了一些有效的 JSON,需要对其进行解析以提取价格。我不会走得很远。

这是 JSON:

{
  "result": "success",
  "prices": {
    "vdc": {
      "monthly": "1.00"
    },
    "network": {
      "private": {
        "monthly": "2.00"
      },
      "public": {
        "\/22 (1,111 IP Addresses)": {
          "monthly": "3.00"
        },
        "\/21 (2,222 IP Addresses)": {
          "monthly": "4.00"
        },
        "\/20 (3,333 IP Addresses)": {
          "monthly": "5.00"
        },
        "\/19 (5,555 IP Addresses)": {
          "monthly": "6.00"
        },
        "\/18 (6,666 IP Addresses)": {
          "monthly": "7.00"
        },
        "\/17 (7,777 IP Addresses)": {
          "monthly": "8.00"
        },
        "\/16 (8,888 IP Addresses)": {
          "monthly": "9.00"
        },
        "\/25 (111 IP Addresses)": {
          "monthly": "10.00"
        },
        "\/26 (55 IP Addresses)": {
          "monthly": "11.00"
        },
        "\/27 (22 IP Addresses)": {
          "monthly": "12.00"
        },
        "\/28 (11 IP Addresses)": {
          "monthly": "13.00"
        },
        "\/29 (5 IP Addresses)": {
          "monthly": "14.00"
        },
        "\/23 (900 IP Addresses)": {
          "monthly": "15.00"
        },
        "\/24 (333 IP Addresses)": {
          "monthly": "16.00"
        }
      }
    },
    "blocks": {
      "22": {
        "monthly": "17.00"
      },
      "21": {
        "monthly": "18.00"
      },
      "20": {
        "monthly": "19.00"
      },
      "19": {
        "monthly": "20.00"
      },
      "18": {
        "monthly": "21.00"
      },
      "17": {
        "monthly": "22.00"
      },
      "16": {
        "monthly": "23.00"
      },
      "25": {
        "monthly": "24.00"
      },
      "26": {
        "monthly": "25.00"
      },
      "27": {
        "monthly": "28.00"
      },
      "28": {
        "monthly": "29.00"
      },
      "29": {
        "monthly": "30.00"
      },
      "23": {
        "monthly": "24.00"
      },
      "24": {
        "monthly": "25.00"
      }
    },
    "server": {
      "cpu": {
        "monthly": "26.00"
      },
      "ram": {
        "monthly": "27.00"
      }
    },
    "volume": {
      "gb": {
        "monthly": "28.00"
      }
    },
    "snapshot": {
      "gb": {
        "monthly": "29.00"
      }
    }
  }
}

在 jsonlint [dot] com 测试和验证。

经过多次尝试、测试、尝试、敲击我的键盘、尝试......这就是我目前拥有的,但它没有产生预期的结果(我会在代码片段之后告诉你那些是什么)。

function gp(x){ 
    for(var i in x){ 
        console.log('700: ',  x[i] );

        if(x[i] != 'success'){
            console.log(733);
            console.log(x[i]);

            for(var j in x[i]){
                console.log(736);
                console.log(x[i][j]);
            }
        }
     }
}

在控制台中,我看到如下内容:

在此处输入图像描述

例如,我真正想看到(或查找或解析到)的是“volume”元素中“gb”的每月价格(或者它是一个项目?)。

理想情况下,我想找到“volume”、“ram”和“cpu”——验证它是 volume、ram 和 cpu——然后得到每月的价格。我确实尝试了一些关于 JSON 解析的事情,但显然,我还没有完全掌握它。

任何帮助将不胜感激。

4

2 回答 2

9

如果你想找到对象“volume”、“ram”和“cpu”,这很简单:

var volume = x.prices.volume;
var ram = x.prices.server.ram;
var cpu = x.prices.server.cpu;

或者您可以直接使用它们:

console.log(x.prices.volume);

如果你想找到每月的价格,那么:

var prices = x.prices;
console.log('volume, monthly=', prices.volume.gb.monthly);
console.log('cpu, monthly=', prices.server.cpu.monthly);
console.log('ram, monthly=', prices.server.ram.monthly);

Javascript 对象非常简单,访问它们只有两种语法:

// If the key you're accessing is a constant (hardcoded):
object.key = value;

// If the key you're accessing is stored in another variable:
var k = "key";
object[k] = value;

// Alternatively:
object["key"] = value;
于 2013-01-04T03:42:15.967 回答
2

我只是有点困惑,但这里有一个例子。将您的 json 保存到一个变量中,然后使用节点名称沿着树向下走(除非没有任何节点名称)。

这应该每月获得音量

 json.prices.volume.gb.monthly 

http://jsfiddle.net/PKUBA/

于 2013-01-04T03:40:47.357 回答