简体   繁体   English

通过 JSON 响应解析 - React Native

[英]Parse Through JSON response - React Native

Sorry for the newb question but I am not sure what I am doing wrong here.很抱歉这个新手问题,但我不确定我在这里做错了什么。 I have a returned JSON string from Firebase of:我有一个从 Firebase 返回的 JSON 字符串:

{ 
   "user":{ 
      "uid":"kjsahdfkpa9asjdf",
      "displayName":null,
      "photoURL":null,
      "email":"test12@test.com",
      "emailVerified":false,
      "phoneNumber":null,
      "isAnonymous":false,
      "tenantId":null,
      "providerData":[  ],
      "apiKey":"asjdf;lkajd;lkfj;laV0",
      "appName":"[DEFAULT]",
      "authDomain":"test.firebaseapp.com",
      "stsTokenManager":{ 
         "apiKey":"Aalskfdeeee9V0",
         "refreshToken":"Aalskdf;lakdfjdjbA",
         "accessToken":"eyJaslkdjf;akldfhg",
         "expirationTime":q48r894q7qq4
      },
      "redirectEventId":null,
      "lastLoginAt":"q8437508234",
      "createdAt":"34534522"
   },
   "credential":null,
   "additionalUserInfo":{ 
      "providerId":"password",
      "isNewUser":true
   },
   "operationType":"signIn"
}

Why can I not simply pull out the apiKey value by doing:为什么我不能简单地通过以下方式提取 apiKey 值:

const response = await config.loginViaRedux(user); //this is what returns the JSON string
const apiKey = response[0].user.stsTokenManager.apiKey;

You are taking the response as array but it's an object.您将响应视为数组,但它是 object。 Ive read the above comments, so i think what you should do is wait for the response object to load.我已经阅读了上面的评论,所以我认为你应该做的是等待响应 object 加载。 So please try this approach and see if it helps:所以请尝试这种方法,看看它是否有帮助:

const response = await config.loginViaRedux(user);
let apiKey = "";
if(response && response.user && response.user.stsTokenManager){
apiKey = response.user.stsTokenManager.apiKey;
}

console.log(apiKey,'apikey')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM