Javascript 通过JSON响应解析-反应本机
很抱歉,这个新问题,但我不确定我在这里做错了什么。我从Firebase返回了一个JSON字符串:Javascript 通过JSON响应解析-反应本机,javascript,json,firebase,react-native,parsing,Javascript,Json,Firebase,React Native,Parsing,很抱歉,这个新问题,但我不确定我在这里做错了什么。我从Firebase返回了一个JSON字符串: { "user":{ "uid":"kjsahdfkpa9asjdf", "displayName":null, "photoURL":null, "email":"test12@test.com", "emailVerified":false, "phoneNumber":null, "isAnonymo
{
"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"
}
为什么我不能简单地通过执行以下操作来提取apiKey值:
const response = await config.loginViaRedux(user); //this is what returns the JSON string
const apiKey = response[0].user.stsTokenManager.apiKey;
您将响应作为数组,但它是一个对象。我读了上面的评论,所以我认为您应该做的是等待响应对象加载。因此,请尝试这种方法,看看它是否有帮助:
const response = await config.loginViaRedux(user);
let apiKey = "";
if(response && response.user && response.user.stsTokenManager){
apiKey = response.user.stsTokenManager.apiKey;
}
console.log(apiKey,'apikey')
因为
response
是一个对象而不是数组。尝试执行response.user.stsTokenManager.apiKey
@PrerakSola当我尝试得到一个错误未定义的不是对象(评估'\u response.user.stsTokenManager.apiKey')
。在提取值之前,我是否必须对JSON.parse()执行任何操作或将其字符串化?为什么在响应之前有\ucode>?另外,“expirationTime”:q48r894q7qq4
在JSON中无效。该值应该在“
@PrerakSola中。出于安全目的,我刚刚删除了带有假数据的实际数据。我不确定为什么会有一个uuu-before响应,因为我的代码中没有这个。响应来自API调用…不幸的是,这不起作用。除非我严格阅读它,否则它根本无法读取响应。当我尝试提取任何值时(即使在我看到它已通过stringized console.log()加载之后),我会返回空值