【问题标题】:How read JSON nested from Angular JS $http service?如何从 Angular JS $http 服务中读取嵌套的 JSON?
【发布时间】:2016-06-03 13:55:45
【问题描述】:

这是我的主要 JSON 文件

{
  "chartType" : ["column", "column", "pie"],
  "chartTitle": ["Cantidad de equipos", "Cantidad de artículos consumibles","Cantidad de empleados a cargo"],
  "yAxisTitle": ["Equipos", "Consumibles", "Empleados"],
  "seriesName": ["conexion_equipos_basededatos_json.php", "conexion_consumibles_basededatos_json.php", "conexion_basededatos_json.php"],
  "seriesData": ["conexion_equipos_basededatos_json.php", "conexion_consumibles_basededatos_json.php", "conexion_basededatos_json.php"]
}

这会在“seriesName”和“seriesData”中加载其他 PHP JSON_ENCODE 文件。

这些 JSON 结果生成键“id”和“nombre”(名称的西班牙语单词)。

如何通过主 JSON 的 Angular Service $http.get 读取这些键的值?

**更新(06/03/2016)**

我在路上!

我使用 angular.fromJson 函数从“seriesName”数组中读取 JSON 对象,如下所示:

var deserialize = angular.FromJson(data);

var objects = deserialize.seriesName;

console.log(objects);

这向我抛出了一个对象数组:

 ["conexion_equipos_basededatos_json.php", "conexion_consumibles_basededatos_json.php", "conexion_basededatos_json.php"]

那么,我如何通过 Angular 读取这些 URL 中包含的对象?

【问题讨论】:

    标签: javascript php angularjs json


    【解决方案1】:

    首先你需要在 PHP 中使用json_decode() 解码 JSON,然后你把结果当作一个对象。

    试试类似的东西;

    $data = xyz; // pass in the JSON object
    $res = json_decode($data); // decode JSON
    $chartType = $res->chartType; // access property as an object
    

    您也可以尝试var_dump($res) 更清楚地了解该结构以及如何访问它;

    【讨论】:

    • 它在 PHP 中很有用,但我的问题与 Angular.JS 有关... json_decode 对于 Angular 的近似函数是什么?
    【解决方案2】:

    您可以执行以下操作。

    $http.get('url_to_json').then(fucntion(response) {
      var myJson = response.data;
      var keys = Object.keys(myJson);
        console.log('Your keys', keys);
    }, fucntion(error) {
    
    });
    

    【讨论】:

      【解决方案3】:

      虽然有按钮,但我可以解决这个问题。

      我创建了一个将 Angular.js、PHP 和 Highcharts 与 Materialize.css 集成的存储库,从外部 JSON 动态添加系列。

      链接:https://github.com/Nullises/DynamicSeriesHighchartsAngular

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-23
        • 1970-01-01
        • 2021-10-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多