c# - xml转json后无法访问数据

标签 c# xml json angularjs

我正在学习 AngularJS。

当我想访问 xml 文件时,我无法做到这一点,我使用了 xml_strjson 此方法将数据 xml 转换为 json 但无法在 html 文件中访问 这是我的代码:

enter code here<!DOCTYPE html>
<html ng-app="">
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js">   </script>
<script type="text/javascript" src="http://demos.amitavroy.com/learningci/assets   /js/xml2json.js" charset="UTF-8"></script>
  <script>
        function customersController($scope, $http) {
        $http.get("http://localhost:13087/Data.xml")
        .success(function (response) {
            $scope.lstComapanies = x2js.xml_str2json(response);
            console.log($scope.lstComapanies);

            alert(response);
        });
    }

</script>
     <title></title>
      </head>
   <body ng-controller="customersController">
     <table>
        <tbody>
               <tr ng-repeat="CompanyModel in lstComapanies">
                <!-- $index for duplicate values -->
                  <td class="border">
                      <label>{{ CompanyModel }}</label>
                </td>

            </tr>
        </tbody>
    </table>

我的 json 数据

{
    "__cnt": 8,
    "CompanyModel": [
        {
            "__cnt": 7,
            "BacklogSum": "646",
            "BacklogSum_asArray": [
                "646"
            ],
            "BillingSum": "607",
            "BillingSum_asArray": [
                "607"
            ],
            "BookingSum": "646",
            "BookingSum_asArray": [
                "646"
            ],
            "CashflowSum": "$-4809038.45",
            "CashflowSum_asArray": [
                "$-4809038.45"
            ],
            "LstCompanies": {
                "__cnt": 1,
                "_i:nil": "true"
            },
            "LstCompanies_asArray": [
                {
                    "__cnt": 1,
                    "_i:nil": "true"
                }
            ],
            "Name": "OPTERNA AM, INC. ",
            "Name_asArray": [
                "OPTERNA AM, INC. "
            ],
            "Number": "2000",
            "Number_asArray": [
                "2000"
            ]
        }
    ],
    "_xmlns:i": "http://www.w3.org/2001/XMLSchema-instance",

}

最佳答案

您需要为您的应用创建一个主模块,然后向其中添加 Controller 。

首先,将脚本更改为:

var myApp = angular.module('myApp', []);

myApp.controller('customersController', ['$scope', '$http',
 function($scope, $http){
    $http.get("http://localhost:13087/Data.xml")
        .success(function (response) {
            $scope.lstComapanies = x2js.xml_str2json(response);
            console.log($scope.lstComapanies);

            alert(response);
    })
}]);

然后正确设置您的应用和 Controller

<body ng-app="myApp">
    <div ng-controller="customersController">
        <table>
            .... etc

此外,如上所述,您需要更改 ng-repeat。

<tr ng-repeat="objects in lstComapanies.CompanyModel">

关于c# - xml转json后无法访问数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27525546/

相关文章:

c# - 用于将字节数组转换为字符串的编码,反之亦然

c# - ServiceStack NuGet 更新 4.0.22 到 4.0.31 导致部署错误

python - 如何使用 python 将数据正确附加到 JSON

java - 震动变换 : Change field value based on the value in a nested element

c# - VB.NET 中代码注释和 XML 注释的不同颜色

c# - 如何将 "IObservable<bool> IsActive"和 "bool IsEnabled"组合在一个订阅中

java - 在 Java 中将 XML 转换为没有属性的 JSON 的简洁有效的方法是什么?

php - 我可以在不自动转义 $url 的情况下使用 simplexml_load_file($url) 吗?

java - 尝试使用自定义适配器填充 ListView

python - 从 Django 模板访问 JSON 嵌套值(数组)