英文:
How to convert the string to JSON in javascript
问题
I can provide translations for the non-code parts of your text:
"When I fetch the data from Firebase Realtime Database, I am getting a string object like below. May I know how can I convert this to a JSON object? How can I get the data like Address: "Add line 1, Add line 2, Street, City, State."? This is for my React Native app.
What I tried:
- When I tried to use JSON.parse, I am getting the error 'JSON Parse error: Unexpected token: o'.
- When I display the data, it is [object object].
- When I try typeof, I am getting the result as an object.
Data that I get from Firebase Realtime Database (It is getting displayed like below when I use JSON.stringify):
user: {"Address":"Add line 1, Add line 2, Street, City, State.","Age":"42","CAppoint":{"0":{"AppointDate":"","ConsultDoc":"","Patient":"C1","Slot":"","Treatment":"","extra1":"","extra2":""}},"PAppoint":{"0":{"AppointDate":"","ConsultDoc":"","Patient":"P1","Slot":"","Treatment":"","extra1":"","extra2":""},"1":{"AppointDate":"","ConsultDoc":"","Patient":"P2","Slot":"","Treatment":"","extra1":"","extra2":""},"2":{"AppointDate":"","ConsultDoc":"","Patient":"P3","Slot":"","Treatment":"","extra1":"","extra2":""},"3":{"AppointDate":"","ConsultDoc":"","Patient":"P4","Slot":"","Treatment":"","extra1":"","extra2":""}},"name":"Zulu","pass":"1234","phone":"+918888888888"}
How I fetch data from Firebase:
const readUserData = async (phoneNumber) => {
try {
const reference = ref(db, 'x123456/' + phoneNumber);
return (await get(reference)).toJSON((snapshot) => {
const data = snapshot.val();
console.log('DATA: ' + data);
}, (error) => {
console.error(error);
return 0;
});
} catch (error) {
console.error(error);
return 0;
}
}
How I call the above function:
try {
exist = await readUserData('+91' + phoneNumber);
console.log('user:' + JSON.stringify(exist));
} catch (error) {
alert(error);
}
Please note that code parts have not been translated as per your request.
英文:
when i fetch the data from firebase real time database, i am getting string object like below. May i know how can i convert this to JSON object?. how can i get the data like Address:"Add line 1, Add line 2, Street,City,State.". ?. This is for my react native app.
What i tried:
- when i tried to use JSON.parse, i am getting the error 'JSON Parse
error: Unexpected token: o'. - when i display the data, it is [object object]
- when i try typeof, i am getting result as object
data that i get from Firebase real time database(It is getting displayed like below when i use JSON.stringify):
user:{"Address":"Add line 1, Add line 2, Street,City,State.","Age":"42","CAppoint":{"0":{"AppointDate":"","ConsultDoc":"","Patient":"C1","Slot":"","Treatment":"","extra1":"","extra2":""}},"PAppoint":{"0":{"AppointDate":"","ConsultDoc":"","Patient":"P1","Slot":"","Treatment":"","extra1":"","extra2":""},"1":{"AppointDate":"","ConsultDoc":"","Patient":"P2","Slot":"","Treatment":"","extra1":"","extra2":""},"2":{"AppointDate":"","ConsultDoc":"","Patient":"P3","Slot":"","Treatment":"","extra1":"","extra2":""},"3":{"AppointDate":"","ConsultDoc":"","Patient":"P4","Slot":"","Treatment":"","extra1":"","extra2":""}},"name":"Zulu","pass":"1234","phone":"+918888888888"}
how i fetch data from firebase:
const readUserData = async (phoneNumber) => {
try {
const reference = ref(db, 'x123456/' + phoneNumber);
return (await get(reference)).toJSON((snapshot) => {
const data = snapshot.val();
console.log('DATA: ' + data);
}, (error) => {
console.error(error);
return 0;
});
}
How i call the above function:
exist=await readUserData('+91'+phoneNumber);
console.log('user:'+ JSON.stringify(exist));
} catch (error) {
alert(error);
}```
</details>
# 答案1
**得分**: 1
以下是翻译好的部分:
也许这是您想要的答案:
```javascript
var obj_str = '{ "Address":"Add line 1, Add line 2, Street, City, State.","Age":"42","CAppoint":{"0":{"AppointDate":"","ConsultDoc":"","Patient":"C1","Slot":"","Treatment":"","extra1":"","extra2":""}},"PAppoint":{"0":{"AppointDate":"","ConsultDoc":"","Patient":"P1","Slot":"","Treatment":"","extra1":"","extra2":""},"1":{"AppointDate":"","ConsultDoc":"","Patient":"P2","Slot":"","Treatment":"","extra1":"","extra2":""},"2":{"AppointDate":"","ConsultDoc":"","Patient":"P3","Slot":"","Treatment":"","extra1":"","extra2":""},"3":{"AppointDate":"","ConsultDoc":"","Patient":"P4","Slot":"","Treatment":"","extra1":"","extra2":""}},"name":"Zulu","pass":"1234","phone":"+918888888888"}';
console.log(obj_str);
var obj = JSON.parse(obj_str);
console.log(obj);
或者...
var obj_str = '{ "user":{"Address":"Add line 1, Add line 2, Street, City, State.","Age":"42","CAppoint":{"0":{"AppointDate":"","ConsultDoc":"","Patient":"C1","Slot":"","Treatment":"","extra1":"","extra2":""}},"PAppoint":{"0":{"AppointDate":"","ConsultDoc":"","Patient":"P1","Slot":"","Treatment":"","extra1":"","extra2":""},"1":{"AppointDate":"","ConsultDoc":"","Patient":"P2","Slot":"","Treatment":"","extra1":"","extra2":""},"2":{"AppointDate":"","ConsultDoc":"","Patient":"P3","Slot":"","Treatment":"","extra1":"","extra2":""},"3":{"AppointDate":"","ConsultDoc":"","Patient":"P4","Slot":"","Treatment":"","extra1":"","extra2":""}},"name":"Zulu","pass":"1234","phone":"+918888888888"}}';
console.log(obj_str);
var obj = JSON.parse(obj_str);
console.log(obj);
JSON 必须以 '{' 开始,以 '}' 结束。
(还需要在 "key" 周围使用引号,例如 "user")。
英文:
Maybe this is the answer you want :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var obj_str = '{"Address":"Add line 1, Add line 2, Street,City,State.","Age":"42","CAppoint":{"0":{"AppointDate":"","ConsultDoc":"","Patient":"C1","Slot":"","Treatment":"","extra1":"","extra2":""}},"PAppoint":{"0":{"AppointDate":"","ConsultDoc":"","Patient":"P1","Slot":"","Treatment":"","extra1":"","extra2":""},"1":{"AppointDate":"","ConsultDoc":"","Patient":"P2","Slot":"","Treatment":"","extra1":"","extra2":""},"2":{"AppointDate":"","ConsultDoc":"","Patient":"P3","Slot":"","Treatment":"","extra1":"","extra2":""},"3":{"AppointDate":"","ConsultDoc":"","Patient":"P4","Slot":"","Treatment":"","extra1":"","extra2":""}},"name":"Zulu","pass":"1234","phone":"+918888888888"}';
console.log(obj_str);
var obj = JSON.parse(obj_str);
console.log(obj);
<!-- end snippet -->
or..
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var obj_str = '{"user":{"Address":"Add line 1, Add line 2, Street,City,State.","Age":"42","CAppoint":{"0":{"AppointDate":"","ConsultDoc":"","Patient":"C1","Slot":"","Treatment":"","extra1":"","extra2":""}},"PAppoint":{"0":{"AppointDate":"","ConsultDoc":"","Patient":"P1","Slot":"","Treatment":"","extra1":"","extra2":""},"1":{"AppointDate":"","ConsultDoc":"","Patient":"P2","Slot":"","Treatment":"","extra1":"","extra2":""},"2":{"AppointDate":"","ConsultDoc":"","Patient":"P3","Slot":"","Treatment":"","extra1":"","extra2":""},"3":{"AppointDate":"","ConsultDoc":"","Patient":"P4","Slot":"","Treatment":"","extra1":"","extra2":""}},"name":"Zulu","pass":"1234","phone":"+918888888888"}}';
console.log(obj_str);
var obj = JSON.parse(obj_str);
console.log(obj);
<!-- end snippet -->
JSON required start with '{' and end with '}'.
(also required 'key' enclosed in quotation marks, like "user")
答案2
得分: 1
"Your function return is an object already."
所以你可以简单地使用你想要的属性:
try{
const myuser = await readUserData('+91' + phoneNumber);
console.log(myuser.Address);
} catch (error) {
alert(error);
}
英文:
Your function return is an object already.
So simply you can use your desired property:
try{
const myuser = await readUserData('+91' + phoneNumber);
console.log(myuser.Address);
} catch (error) {
alert(error);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论