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

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

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 = &#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;;

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 = &#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;;

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(&#39;+91&#39; + phoneNumber);
    console.log(myuser.Address);
} catch (error) {
    alert(error);
}

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:

确定