【问题标题】:How to retrieve specific data from json,the json values comes from a url如何从 json 中检索特定数据,json 值来自 url
【发布时间】:2018-01-02 10:43:52
【问题描述】:

我已经尝试过针对同一问题的其他问题来从 JSON 中检索数据。但它们都不适合我,因为我的 JSON 数据与其他数据不同。

也许我的问题听起来和其他问题一样,但我什么都试过了。

[  
   {  
      "bHeader":{  
         "ei":"NSE",
         "seg":"I"
      },
      "cNetChangeIndicator":"\u0000",
      "fClosingIndex":10558.5,
      "fHighIndexValue":10532.0,
      "fIndexValue":10469.0,
      "fLowIndexValue":10438.5,
      "fOpeningIndex":10499.5,
      "fPercentChange":-0.85,
      "sIndexName":"962450",
      "fChange":-89.5,
      "iIdxId":311
   }
]

请帮我找出值

fIndexValue
fChange
fPercentChange.

【问题讨论】:

  • 预期输出是什么?您的尝试遇到了什么问题?
  • 问题是我没有得到 fIndexValue,fChange,fPercentChange 的值。我试过这个链接 "stackoverflow.com/questions/10443466/…我的 json 格式不同
  • 您也可以分享一下您的尝试吗?

标签: javascript php jquery arrays json


【解决方案1】:

尝试以下方法:

var data = [{"bHeader":{"ei":"NSE","seg":"I"},"cNetChangeIndicator":"\u0000","fClosingIndex":10558.5,"fHighIndexValue":10532.0,"fIndexValue":10469.0,"fLowIndexValue":10438.5,"fOpeningIndex":10499.5,"fPercentChange":-0.85,"sIndexName":"962450","fChange":-89.5,"iIdxId":311}];

data.forEach(function(item){
  console.log('fIndexValue:'+ item.fIndexValue);
  console.log('fChange:'+ item.fChange);
  console.log('fPercentChange:'+ item.fPercentChange);
});

【讨论】:

    【解决方案2】:

    更新

    您可以使用json_decode(json_string, true) php 函数,如下所示

    $str = '[{"bHeader":{"ei":"NSE","seg":"I"},"cNetChangeIndicator":"\u0000","fClosingIndex":10558.5,"fHighIndexValue":10532.0,"fIndexValue":10469.0,"fLowIndexValue":10438.5,"fOpeningIndex":10499.5,"fPercentChange":-0.85,"sIndexName":"962450","fChange":-89.5,"iIdxId":311}]';
    
    $arr = json_decode($str, true);
    
    print_r($arr);
    
    echo $arr[0]['fIndexValue'];
    echo $arr[0]['fChange'];
    echo $arr[0]['fPercentChange'];
    

    每 30 秒刷新一次页面。在下面写代码。

    // write the function
    function refresh($time)
    {
        $current_url = $_SERVER[ 'REQUEST_URI' ];
        return header( "Refresh: " . $time . "; URL=$current_url" );
    }
    
    // call the function in the appropriate place
    refresh(30);
    // this refreshes page after 4 seconds
    

    【讨论】:

    • 这不会打印任何东西 echo $arr[0]['fIndexValue'];回声 $arr[0]['fChange']; echo $arr[0]['fPercentChange'];$str 的值也是动态的,它来自 url,所以我需要在 30 秒后刷新那个 url。我使用了 Curl,但它没有完成任务。我的电子邮件 ID is- anindya12.it@gmail.com
    • 我得到的是总数组而不是这些值分别 echo $arr[0]['fIndexValue'];回声 $arr[0]['fChange']; echo $arr[0]['fPercentChange'];
    • in this print_r($arr);数组你可以看到你在json中传递的数组中的整个值。你可以使用这个数组获得价值
    【解决方案3】:

    你尝试过 JSON.parse(request) 吗?您将能够作为对象访问

    【讨论】:

    • 嗨,Pablo Fuentes,你能解释一下吗,因为我不太了解 Json
    【解决方案4】:

    您可以执行以下操作:

    $data = '[  
       {  
          "bHeader":{  
             "ei":"NSE",
             "seg":"I"
          },
          "cNetChangeIndicator":"\u0000",
          "fClosingIndex":10558.5,
          "fHighIndexValue":10532.0,
          "fIndexValue":10469.0,
          "fLowIndexValue":10438.5,
          "fOpeningIndex":10499.5,
          "fPercentChange":-0.85,
          "sIndexName":"962450",
          "fChange":-89.5,
          "iIdxId":311
       }
    ]';
    
    $data = json_decode($data);
    foreach ($data as $d){
       echo  $d->fIndexValue;
       echo  $d->fChange;
       echo  $d->fPercentChange;
    }
    

    【讨论】:

      【解决方案5】:

      你的 json 是二维的,所以你应该尝试类似

      data[0]['fHighIndexValue']

      寻找小提琴

      http://jsfiddle.net/scnx3vh4/

      【讨论】:

        猜你喜欢
        • 2019-01-12
        • 2014-11-27
        • 2016-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多