无法从解析的JSON中记录对象详细信息?

时间:2017-08-22 03:53:19

标签: javascript php json

我记录以下内容没有问题:

<h1>Order List</h1>

<ul> 
    <%orders.forEach(function(order){%>
        <li><%console.log(order.ShippingAddress);%></li>
    <%});%>
</ul>

但是当我尝试记录下来时:

<h1>Order List</h1>

<ul> 
    <%orders.forEach(function(order){%>
        <li><%console.log(order.ShippingAddress.Name);%></li>
    <%});%>
</ul>

我收到错误:

Cannot read property 'Name' of undefined

我必须忽略一些非常简单的事情,因为&#34;姓名&#34;显然存在......我只是没有正确设置。有什么想法吗?

以下是每个订单的数据:

{ AmazonOrderId: '99999',
  PurchaseDate: 'date',
  LastUpdateDate: 'date',
  OrderStatus: 'Shipped',
  FulfillmentChannel: 'MFN',
  SalesChannel: 'Amazon.com',
  ShipServiceLevel: 'Std US D2D Dom',
  ShippingAddress: 
    { Name: 'name here',
      AddressLine1: 'address here',
      City: 'city',
      StateOrRegion: 'state',
      PostalCode: '999999',
      CountryCode: 'US' }

我可以通过以下方式使用pHp来实现这一点:

<?php
    $string = file_get_contents('./data/store.json');
    $data = json_decode($string, true);


    foreach($data[ListOrdersResult][Orders][Order] as $order) {
        print_r("<li>".$order[ShippingAddress][Name]."</li>");
    }
?>

1 个答案:

答案 0 :(得分:1)

您确定第一个代码段实际上包含带有数据的ShippingAddress吗?

可能有1个特定订单(或更多)没有送货信息?在尝试显示名称之前尝试删除console.log(order.AmazonOrderId),然后您将看到它停止的顺序。

相关问题