【问题标题】:Parsing from a JSON and show in angular从 JSON 解析并以角度显示
【发布时间】:2016-01-05 17:01:16
【问题描述】:

我有两个 JSON 格式的数据。我如何解析它们并以角度显示?我真的很困惑如何进行。同样在第二个数据中,plan_id 指的是“计划”数据。我怎样才能制作一个小的查找对象,它将使用plan_id 查找“计划”。下面是json文件和我的索引

{  
   "plans":[  
      {  
         "id":1,
         "code":"NEXT_DAY_500gm",
         "name":"Next Day less than 500gm",
         "cost":55,
         "duration":24,
         "min_weight":0,
         "max_weight":500,
         "COD":1,
         "created_at":"-0001-11-30 00:00:00",
         "updated_at":"-0001-11-30 00:00:00"
      },
      {  
         "id":2,
         "code":"NEXT_DAY_500gm_1kg",
         "name":"Next Day 500gm to 1kg",
         "cost":70,
         "duration":24,
         "min_weight":501,
         "max_weight":1000,
         "COD":1,
         "created_at":"-0001-11-30 00:00:00",
         "updated_at":"-0001-11-30 00:00:00"
      },
      {  
         "id":3,
         "code":"NEXT_DAY_1kg_2kg",
         "name":"Next Day 1kg to 2kg",
         "cost":95,
         "duration":24,
         "min_weight":1001,
         "max_weight":2000,
         "COD":1,
         "created_at":"-0001-11-30 00:00:00",
         "updated_at":"-0001-11-30 00:00:00"
      },
      {  
         "id":4,
         "code":"SAME_DAY_500gm",
         "name":"Same Day less than 500gm",
         "cost":95,
         "duration":8,
         "min_weight":0,
         "max_weight":500,
         "COD":1,
         "created_at":"-0001-11-30 00:00:00",
         "updated_at":"-0001-11-30 00:00:00"
      },
      {  
         "id":5,
         "code":"SAME_DAY_500gm_1kg",
         "name":"Same Day 500gm to 1kg",
         "cost":130,
         "duration":8,
         "min_weight":501,
         "max_weight":1000,
         "COD":1,
         "created_at":"-0001-11-30 00:00:00",
         "updated_at":"-0001-11-30 00:00:00"
      },
      {  
         "id":6,
         "code":"SAME_DAY_1kg_2kg",
         "name":"Same Day 1kg to 2kg",
         "cost":165,
         "duration":8,
         "min_weight":1001,
         "max_weight":2000,
         "COD":1,
         "created_at":"-0001-11-30 00:00:00",
         "updated_at":"-0001-11-30 00:00:00"
      }
   ]
}



[{"id":23,"sender_id":3,"courier_id":0,"deliverer_id":0,"status":"PENDING","consignment_id":"55C0A999","sender_name":"asdasdas d","cost":"40","package_description":"asdasdasd","sender_address":"asdasdasd","sender_longitude":"","sender_latitude":"","sender_number":null,"receiver_address":"adasdasd","receiver_name":"Shifat Adnan","receiver_number":"01713206053","receiver_longitude":"","receiver_latitude":"","current_longitude":"","current_latitude":"","deliverer":null,"recipient_pays":0,"instructions":"2332323","payment_status":null,"cod":0,"recipient_zone_id":2,"plan_id":2,"store_id":null,"storeproduct_id":null,"created_at":"2015-08-04 12:01:29"},{"id":28,"sender_id":5,"courier_id":0,"deliverer_id":0,"status":"COMPLETED","consignment_id":"10F317","sender_name":"bleh","cost":"398","package_description":"Business Order","sender_address":"bleh","sender_longitude":"bleh","sender_latitude":"bleh","sender_number":"bleh","receiver_address":"kjakdjlsdjlaksdjl","receiver_name":"Nigga","receiver_number":"010203001","receiver_longitude":"","receiver_latitude":"","current_longitude":"bleh","current_latitude":"bleh","deliverer":null,"recipient_pays":0,"instructions":"jslklsdflkj","payment_status":null,"cod":0,"recipient_zone_id":null,"plan_id":4,"store_id":null,"storeproduct_id":null,"created_at":"2015-10-04 09:36:23"},{"id":34,"sender_id":5,"courier_id":0,"deliverer_id":0,"status":"PENDING","consignment_id":"137F24","sender_name":"bleh","cost":"3223","package_description":"Business Order","sender_address":"bleh","sender_longitude":"bleh","sender_latitude":"bleh","sender_number":"bleh","receiver_address":"kasdlkaskd asldka","receiver_name":"FAAskl","receiver_number":"199102909","receiver_longitude":"","receiver_latitude":"","current_longitude":"bleh","current_latitude":"bleh","deliverer":null,"recipient_pays":0,"instructions":"dsdsdf","payment_status":null,"cod":0,"recipient_zone_id":2,"plan_id":3,"store_id":null,"storeproduct_id":null,"created_at":"2015-10-06 07:58:28"},{"id":35,"sender_id":5,"courier_id":0,"deliverer_id":0,"status":"COMPLETED","consignment_id":"13C476","sender_name":"bleh","cost":"3310","package_description":"Business Order","sender_address":"bleh","sender_longitude":"bleh","sender_latitude":"bleh","sender_number":"bleh","receiver_address":"sdfs dfasdfasdfasdf","receiver_name":"sfasd","receiver_number":"224","receiver_longitude":"","receiver_latitude":"","current_longitude":"bleh","current_latitude":"bleh","deliverer":null,"recipient_pays":0,"instructions":"dasdasd","payment_status":null,"cod":0,"recipient_zone_id":2,"plan_id":1,"store_id":1,"storeproduct_id":1,"created_at":"2015-10-06 12:54:14"},{"id":36,"sender_id":5,"courier_id":0,"deliverer_id":0,"status":"COMPLETED","consignment_id":"13C4B3","sender_name":"bleh","cost":"330","package_description":"Business Order","sender_address":"bleh","sender_longitude":"bleh","sender_latitude":"bleh","sender_number":"bleh","receiver_address":"sdfs dfasdfasdfasdf","receiver_name":"Some full name","receiver_number":"224","receiver_longitude":"","receiver_latitude":"","current_longitude":"bleh","current_latitude":"bleh","deliverer":null,"recipient_pays":0,"instructions":"dasdasd","payment_status":null,"cod":0,"recipient_zone_id":3,"plan_id":4,"store_id":1,"storeproduct_id":2,"created_at":"2015-10-06 12:55:15”}]

index.html

<!DOCTYPE HTML>
<html>
    <head>
        <title>Reports </title>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>

</head>
<body ng-app="reportApp" ng-controller="mainCtrl">
    <table>

    </table>

</body>

我不知道下一步该做什么。如何解析和显示json?请帮助或提供任何线索。

【问题讨论】:

  • 一个很好的线索是通过文档站点上的tutorial。还对使用 javascript 数组和对象进行一些研究。老实说,您要问的是how do I use angular,要使用它,您需要对数组有基本的了解
  • Googling your question title 找到Parse JSON and show data in Angular,这应该会给你一些提示。请考虑在此处发布您的问题之前使用谷歌搜索。
  • @charlietfl 官方教程实际上已经很老了(很遗憾),我推荐codeschool.com 教程,因为它包含了大多数现代角度最佳实践。
  • 我已经看到这个问题@dominik。但实际上我不需要任何 http 请求。我有我的 json 数据。只想解析它们并显示它们。
  • @charlietfl 的做法已经过时了。它只是教了很多我们经常在这里看到的不良做法,因为官方教程似乎是新人的好去处,但实际上并非如此

标签: javascript json angularjs


【解决方案1】:

将您的 json 对象分配给某个范围对象变量 注意:请在需要的地方照顾终结符 (;)。 希望这可以让您知道如何做。

$scope.jsonScopeObject={  
   "plans":[  
      {  
         "id":1,
         "code":"NEXT_DAY_500gm",
         "name":"Next Day less than 500gm",
         "cost":55,
         "duration":24,
         "min_weight":0,
         "max_weight":500,
         "COD":1,
         "created_at":"-0001-11-30 00:00:00",
         "updated_at":"-0001-11-30 00:00:00"
      },
      {  
         "id":2,
         "code":"NEXT_DAY_500gm_1kg",
         "name":"Next Day 500gm to 1kg",
         "cost":70,
         "duration":24,
         "min_weight":501,
         "max_weight":1000,
         "COD":1,
         "created_at":"-0001-11-30 00:00:00",
         "updated_at":"-0001-11-30 00:00:00"
      },
      {  
         "id":3,
         "code":"NEXT_DAY_1kg_2kg",
         "name":"Next Day 1kg to 2kg",
         "cost":95,
         "duration":24,
         "min_weight":1001,
         "max_weight":2000,
         "COD":1,
         "created_at":"-0001-11-30 00:00:00",
         "updated_at":"-0001-11-30 00:00:00"
      },
      {  
         "id":4,
         "code":"SAME_DAY_500gm",
         "name":"Same Day less than 500gm",
         "cost":95,
         "duration":8,
         "min_weight":0,
         "max_weight":500,
         "COD":1,
         "created_at":"-0001-11-30 00:00:00",
         "updated_at":"-0001-11-30 00:00:00"
      },
      {  
         "id":5,
         "code":"SAME_DAY_500gm_1kg",
         "name":"Same Day 500gm to 1kg",
         "cost":130,
         "duration":8,
         "min_weight":501,
         "max_weight":1000,
         "COD":1,
         "created_at":"-0001-11-30 00:00:00",
         "updated_at":"-0001-11-30 00:00:00"
      },
      {  
         "id":6,
         "code":"SAME_DAY_1kg_2kg",
         "name":"Same Day 1kg to 2kg",
         "cost":165,
         "duration":8,
         "min_weight":1001,
         "max_weight":2000,
         "COD":1,
         "created_at":"-0001-11-30 00:00:00",
         "updated_at":"-0001-11-30 00:00:00"
      }
   ]
}

<!DOCTYPE HTML>
    <html>
        <head>
            <title>Pathao Reports </title>
            <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>

    </head>
    <body ng-app="reportApp" ng-controller="mainCtrl">
        <table>
             <th>head1</th>
             <th>head2</th>


        </table>
        <tbody>
              <tr ng-repeat="data in jsonScopeObject.plans">
                 <td>{{data.id}}</td>
                 <td>{{data.code}}</td>

              <tr>
        </tbody>



</body>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 2018-06-11
    • 2019-10-15
    • 1970-01-01
    相关资源
    最近更新 更多