【问题标题】:JSON response cannot extract variablesJSON 响应无法提取变量
【发布时间】:2021-02-10 15:28:55
【问题描述】:

react-native 和 typescript 的全新功能! 我在提取 JSON 响应时遇到了一些麻烦。我将提取响应并将其放入一个类中,如下所示。

这里是请求代码

    let notifications: INotification[]

    notifications = (await Requests.GET('notification/user/test-user-1', accessToken));

这是课程

      export interface INotification {
          id: string;
          senderId: string;
          receiverId: string;
          text: string;
          isSeen: boolean;
          type: string;
          timestamp: string;
      }

这是邮递员的回复

{
    "notifications": [
        {
            "pk": "user-1",
            "sk": "notification1234",
            "entity": "notification",
            "id": "id number",
            "senderId": "test-user-2",
            "receiverId": "test-user-1",
            "text": "Test notifications",
            "isSeen": false,
            "type": 2
        }
    ]
}

这是来自控制台的响应

  { notifications: 
       [ { pk: 'user#test-user-1',
           sk: 'notification1234',
           entity: 'notification',
           id: 'id number',
           senderId: 'test-user-2',
           receiverId: 'test-user-1',
           text: 'Test notifications',
           isSeen: false,
           type: 2 } ] 
}

我希望能够写出来:

console.log("TEXT: ",notifications[0].text )

并得到响应:“文本:测试通知”

欢迎任何帮助!

【问题讨论】:

  • 您能否编辑问题,添加您当前正在尝试的内容、尝试的结果以及与您想要的结果有何不同?

标签: json typescript react-native react-native-android react-native-ios


【解决方案1】:

数据在数组中,需要先传数组

console.log("TEXT: ", notifications[0].text);

【讨论】:

  • 您好!谢谢您的答复。当我运行时,我得到了这个:
  • undefined 不是一个对象(评估 'notifications[0].text')
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-04
  • 1970-01-01
  • 1970-01-01
  • 2016-03-28
相关资源
最近更新 更多