如何在JavaScript中将字符串转换为JSON

huangapple go评论91阅读模式
英文:

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):

  1. 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:

  1. const readUserData = async (phoneNumber) => {
  2. try {
  3. const reference = ref(db, 'x123456/' + phoneNumber);
  4. return (await get(reference)).toJSON((snapshot) => {
  5. const data = snapshot.val();
  6. console.log('DATA: ' + data);
  7. }, (error) => {
  8. console.error(error);
  9. return 0;
  10. });
  11. } catch (error) {
  12. console.error(error);
  13. return 0;
  14. }
  15. }

How I call the above function:

  1. try {
  2. exist = await readUserData('+91' + phoneNumber);
  3. console.log('user:' + JSON.stringify(exist));
  4. } catch (error) {
  5. alert(error);
  6. }

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):

  1. 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:

  1. const readUserData = async (phoneNumber) => {
  2. try {
  3. const reference = ref(db, 'x123456/' + phoneNumber);
  4. return (await get(reference)).toJSON((snapshot) => {
  5. const data = snapshot.val();
  6. console.log('DATA: ' + data);
  7. }, (error) => {
  8. console.error(error);
  9. return 0;
  10. });
  11. }

How i call the above function:

  1. exist=await readUserData('+91'+phoneNumber);
  2. console.log('user:'+ JSON.stringify(exist));
  3. } catch (error) {
  4. alert(error);
  5. }```
  6. </details>
  7. # 答案1
  8. **得分**: 1
  9. 以下是翻译好的部分:
  10. 也许这是您想要的答案:
  11. ```javascript
  12. 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"}';
  13. console.log(obj_str);
  14. var obj = JSON.parse(obj_str);
  15. console.log(obj);

或者...

  1. 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"}}';
  2. console.log(obj_str);
  3. var obj = JSON.parse(obj_str);
  4. 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 -->

  1. var obj_str = &#39;{&quot;Address&quot;:&quot;Add line 1, Add line 2, Street,City,State.&quot;,&quot;Age&quot;:&quot;42&quot;,&quot;CAppoint&quot;:{&quot;0&quot;:{&quot;AppointDate&quot;:&quot;&quot;,&quot;ConsultDoc&quot;:&quot;&quot;,&quot;Patient&quot;:&quot;C1&quot;,&quot;Slot&quot;:&quot;&quot;,&quot;Treatment&quot;:&quot;&quot;,&quot;extra1&quot;:&quot;&quot;,&quot;extra2&quot;:&quot;&quot;}},&quot;PAppoint&quot;:{&quot;0&quot;:{&quot;AppointDate&quot;:&quot;&quot;,&quot;ConsultDoc&quot;:&quot;&quot;,&quot;Patient&quot;:&quot;P1&quot;,&quot;Slot&quot;:&quot;&quot;,&quot;Treatment&quot;:&quot;&quot;,&quot;extra1&quot;:&quot;&quot;,&quot;extra2&quot;:&quot;&quot;},&quot;1&quot;:{&quot;AppointDate&quot;:&quot;&quot;,&quot;ConsultDoc&quot;:&quot;&quot;,&quot;Patient&quot;:&quot;P2&quot;,&quot;Slot&quot;:&quot;&quot;,&quot;Treatment&quot;:&quot;&quot;,&quot;extra1&quot;:&quot;&quot;,&quot;extra2&quot;:&quot;&quot;},&quot;2&quot;:{&quot;AppointDate&quot;:&quot;&quot;,&quot;ConsultDoc&quot;:&quot;&quot;,&quot;Patient&quot;:&quot;P3&quot;,&quot;Slot&quot;:&quot;&quot;,&quot;Treatment&quot;:&quot;&quot;,&quot;extra1&quot;:&quot;&quot;,&quot;extra2&quot;:&quot;&quot;},&quot;3&quot;:{&quot;AppointDate&quot;:&quot;&quot;,&quot;ConsultDoc&quot;:&quot;&quot;,&quot;Patient&quot;:&quot;P4&quot;,&quot;Slot&quot;:&quot;&quot;,&quot;Treatment&quot;:&quot;&quot;,&quot;extra1&quot;:&quot;&quot;,&quot;extra2&quot;:&quot;&quot;}},&quot;name&quot;:&quot;Zulu&quot;,&quot;pass&quot;:&quot;1234&quot;,&quot;phone&quot;:&quot;+918888888888&quot;}&#39;;
  2. console.log(obj_str);
  3. var obj = JSON.parse(obj_str);
  4. console.log(obj);

<!-- end snippet -->

or..

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. var obj_str = &#39;{&quot;user&quot;:{&quot;Address&quot;:&quot;Add line 1, Add line 2, Street,City,State.&quot;,&quot;Age&quot;:&quot;42&quot;,&quot;CAppoint&quot;:{&quot;0&quot;:{&quot;AppointDate&quot;:&quot;&quot;,&quot;ConsultDoc&quot;:&quot;&quot;,&quot;Patient&quot;:&quot;C1&quot;,&quot;Slot&quot;:&quot;&quot;,&quot;Treatment&quot;:&quot;&quot;,&quot;extra1&quot;:&quot;&quot;,&quot;extra2&quot;:&quot;&quot;}},&quot;PAppoint&quot;:{&quot;0&quot;:{&quot;AppointDate&quot;:&quot;&quot;,&quot;ConsultDoc&quot;:&quot;&quot;,&quot;Patient&quot;:&quot;P1&quot;,&quot;Slot&quot;:&quot;&quot;,&quot;Treatment&quot;:&quot;&quot;,&quot;extra1&quot;:&quot;&quot;,&quot;extra2&quot;:&quot;&quot;},&quot;1&quot;:{&quot;AppointDate&quot;:&quot;&quot;,&quot;ConsultDoc&quot;:&quot;&quot;,&quot;Patient&quot;:&quot;P2&quot;,&quot;Slot&quot;:&quot;&quot;,&quot;Treatment&quot;:&quot;&quot;,&quot;extra1&quot;:&quot;&quot;,&quot;extra2&quot;:&quot;&quot;},&quot;2&quot;:{&quot;AppointDate&quot;:&quot;&quot;,&quot;ConsultDoc&quot;:&quot;&quot;,&quot;Patient&quot;:&quot;P3&quot;,&quot;Slot&quot;:&quot;&quot;,&quot;Treatment&quot;:&quot;&quot;,&quot;extra1&quot;:&quot;&quot;,&quot;extra2&quot;:&quot;&quot;},&quot;3&quot;:{&quot;AppointDate&quot;:&quot;&quot;,&quot;ConsultDoc&quot;:&quot;&quot;,&quot;Patient&quot;:&quot;P4&quot;,&quot;Slot&quot;:&quot;&quot;,&quot;Treatment&quot;:&quot;&quot;,&quot;extra1&quot;:&quot;&quot;,&quot;extra2&quot;:&quot;&quot;}},&quot;name&quot;:&quot;Zulu&quot;,&quot;pass&quot;:&quot;1234&quot;,&quot;phone&quot;:&quot;+918888888888&quot;}}&#39;;
  2. console.log(obj_str);
  3. var obj = JSON.parse(obj_str);
  4. 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."

所以你可以简单地使用你想要的属性:

  1. try{
  2. const myuser = await readUserData('+91' + phoneNumber);
  3. console.log(myuser.Address);
  4. } catch (error) {
  5. alert(error);
  6. }

更多信息

英文:

Your function return is an object already.

So simply you can use your desired property:

  1. try{
  2. const myuser = await readUserData(&#39;+91&#39; + phoneNumber);
  3. console.log(myuser.Address);
  4. } catch (error) {
  5. alert(error);
  6. }

More Info

huangapple
  • 本文由 发表于 2023年5月14日 15:11:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定