-1

我有一个向 RESTful Web 服务(通过 php 和 curl)发送请求的页面。该页面接收一个 JSON 对象作为响应。我正在尝试使用返回的数据填写一些表单字段。在我的页面上回显的对象 javascript 如下所示:

var obj = {
  "NPI": [
{
  "NPI": "123456789",
  "EntityType": "Individual",
  "IsSoleProprietor": "N",
  "LastName": "Smith",
  "FirstName": "John",
  "MiddleName": "D",
  "NameSuffix": "JR.",
  "Credential": "MD",
  "FirstLineMailingAddress": "PO BOX 123",
  "MailingAddressCityName": "SCOTTSDALE",
  "MailingAddressStateName": "AZ",
  "MailingAddressPostalCode": "85255-0162",
  "MailingAddressCountryCode": "US",
  "MailingAddressTelephoneNumber": "888-123-4567",
  "MailingAddressFaxNumber": "888-123-4567",
  "FirstLinePracticeLocationAddress": "123 DR",
  "SecondLinePracticeLocationAddress": "#278",
  "PracticeLocationAddressCityName": "SCOTTSDALE",
  "PracticeLocationAddressStateName": "AZ",
  "PracticeLocationAddressPostalCode": "85266-2273",
  "PracticeLocationAddressCountryCode": "US",
  "PracticeLocationAddressTelephoneNumber": "888-123-4567",
  "PracticeLocationAddressFaxNumber": "888-123-4567",
  "EnumerationDate": "09/20/2006",
  "LastUpdateDate": "02/07/2011",
  "GenderCode": "M",
  "Gender": "Male",

    }
  ]
};
alert(obj.NPI.NPI);

访问这些对象属性的语法是什么。下面的警告语句不起作用(它警告“未定义”)。

谢谢您的帮助

4

3 回答 3

0

试试obj.NPI[0].NPI 这个应该可以

问题是第一个 NPI 是一个数组而不是一个对象,所以因为存在 [] 所以你应该选择数组中的哪个元素

于 2012-09-15T00:22:26.240 回答
0

obj.NPI是一个对象数组。

于 2012-09-15T00:22:56.157 回答
0

试试这个

console.log(obj.NPI[0].NPI);
console.log(obj.NPI[0].EntityType);
//etc

你的结构是这样的

{// Start of object
  "NPI": //First element, access it as obj.NPI
      [ // NPI is an array
        { // First element of the array is an object
           "NPI": "123456789", // Then access each element of this object using dot notation
           "EntityType": "Individual",
           ................................
于 2012-09-15T00:23:04.490 回答