是否可以从提到的JSON中提取特定值?

时间:2017-03-21 10:37:21

标签: jquery

以下Rest API调用的响应为

http://techpaisa.com/supres/m&m/

{
    "s": "M&M",
    "c": 1300.2,
    "e": false,
    "d": "20 March 2017",
    "l": [
        [1508.95, "52 Week High"],
        [1361.0, "Fibonacci Retracement Resisance"],
        [1360.0, "Call Option with Maximum Open Interest"],
        [1332.3500000000001, "R3 (Pivot Point)"],
        [1331.5685000000003, "200-Day Simple Moving Average"],
        [1326.8818166890858, "Average True Range (Upper Level)"],
        [1324.4000000000001, "R2 (Pivot Point)"],
        [1314.8699999999994, "20-Day Simple Moving Average"],
        [1312.3000000000002, "R1 (Pivot Point)"],
        [1307.73139784, "20-Day Exponential Moving Average"],
        [1304.3500000000001, "Pivot"],
        [1300.2, "Previous Closing Price (CP)"],
        [1300.0, "Put Option with Maximum Open Interest"],
        [1292.82354169, "200-Day Exponential Moving Average"],
        [1292.2500000000002, "S1 (Pivot Point)"],
        [1290.8456999999999, "Fibonacci Retracement (38% Level)"],
        [1284.7771330400001, "50-Day Exponential Moving Average"],
        [1284.3000000000002, "S2 (Pivot Point)"],
        [1273.5181833109143, "Average True Range (Lower Level)"],
        [1273.4080000000001, "50-Day Simple Moving Average"],
        [1272.2000000000003, "S3 (Pivot Point)"],
        [1269.175, "Fibonacci Retracement (50% Level)"],
        [1247.5043000000001, "Fibonacci Retracement (61% Level)"],
        [1177.3499999999999, "Fibonacci Retracement Support"],
        [1141.4000000000001, "52 Week Low"]
    ]
}

我正在尝试提取股票的支持值

var symbol = jsonRes.s;   // Symbol  

是否可以从上面的JSON ??

中提取这些值(支持)

S1(数据透视点),S2(数据透视点),S3(数据透视点)

http://jsfiddle.net/cod7ceho/467/

2 个答案:

答案 0 :(得分:2)

静态地我们可以找到。

 var ll = jsonRes.l;
 var s1 = ll[14];
 var s2 = ll[17];
 var s3 = ll[20];
console.log(s1[1])
console.log(s2[1])
console.log(s3[1])

webapi结果是我们可以做的常数。

答案 1 :(得分:1)

您可以使用startsWith()& endsWith()方法如下。

var jsonRes = {
    "s": "M&M",
    "c": 1300.2,
    "e": false,
    "d": "20 March 2017",
    "l": [
        [1508.95, "52 Week High"],
        [1361.0, "Fibonacci Retracement Resisance"],
        [1360.0, "Call Option with Maximum Open Interest"],
        [1332.3500000000001, "R3 (Pivot Point)"],
        [1331.5685000000003, "200-Day Simple Moving Average"],
        [1326.8818166890858, "Average True Range (Upper Level)"],
        [1324.4000000000001, "R2 (Pivot Point)"],
        [1314.8699999999994, "20-Day Simple Moving Average"],
        [1312.3000000000002, "R1 (Pivot Point)"],
        [1307.73139784, "20-Day Exponential Moving Average"],
        [1304.3500000000001, "Pivot"],
        [1300.2, "Previous Closing Price (CP)"],
        [1300.0, "Put Option with Maximum Open Interest"],
        [1292.82354169, "200-Day Exponential Moving Average"],
        [1292.2500000000002, "S1 (Pivot Point)"],
        [1290.8456999999999, "Fibonacci Retracement (38% Level)"],
        [1284.7771330400001, "50-Day Exponential Moving Average"],
        [1284.3000000000002, "S2 (Pivot Point)"],
        [1273.5181833109143, "Average True Range (Lower Level)"],
        [1273.4080000000001, "50-Day Simple Moving Average"],
        [1272.2000000000003, "S3 (Pivot Point)"],
        [1269.175, "Fibonacci Retracement (50% Level)"],
        [1247.5043000000001, "Fibonacci Retracement (61% Level)"],
        [1177.3499999999999, "Fibonacci Retracement Support"],
        [1141.4000000000001, "52 Week Low"]
    ]
};

var support=[];
jsonRes.l.forEach(function(item){
    var text = item[1];
    if(text.startsWith('S') && text.endsWith('(Pivot Point)'))
        support.push({value: item[0], text: item[1]});
});
console.log(support);