如何将 JSON stringify 转换为 JSON?

7 浏览
0 Comments

如何将 JSON stringify 转换为 JSON?

如何将JSON stringify转换为JSON格式?\n我的代码:\nvar socket = io.connect(\'URL\');\nsocket.on(\'trades\', function (tradeMsg) {\n // console.log(tradeMsg);\n document.getElementById(\'trade\').innerHTML = JSON.stringify(tradeMsg)\n}).\n**我需要的输出JSON格式**:例如:\n{\n \"coin\": \"SPHR\",\n \"exchange_id\": \"bittrex\",\n \"market_id\": \"BTC_SPHR\",\n \"message\": {\n \"coin\": \"SPHR\",\n \"msg\": {\n \"cap24hrChange\": -12.71,\n \"long\": \"Sphere\",\n \"mktcap\": 6821313,\n \"perc\": -12.71,\n \"price\": 2.8884325,\n \"shapeshift\": false,\n \"short\": \"SPHR\",\n \"supply\": 3082940,\n \"usdVolume\": 2838.85,\n \"volume\": 2838.85,\n \"vwapData\": 2.2126,\n \"vwapDataBTC\": 2.2126\n }\n },\n \"msg\": {\n \"cap24hrChange\": -12.71,\n \"long\": \"Sphere\",\n \"mktcap\": 6821313,\n \"perc\": -12.71,\n \"price\": 2.8884325,\n \"shapeshift\": false,\n \"short\": \"SPHR\",\n \"supply\": 3082940,\n \"usdVolume\": 2838.85,\n \"volume\": 2838.85,\n \"vwapData\": 2.2126,\n \"vwapDataBTC\": 2.2126\n },\n \"trade\": {\n \"data\": {\n \"exchange_id\": \"bittrex\",\n \"market_id\": \"BTC_SPHR\",\n \"price\": 2040.8163,\n \"raw\": {\n \"Id\": 6508626,\n \"TimeStamp\": \"2017-10-24T08:48:51.533\",\n \"Quantity\": 9.252431,\n \"Price\": 4.9E-4,\n \"Total\": 0.00453369,\n \"FillType\": \"FILL\",\n \"OrderType\": \"SELL\"\n },\n \"timestamp_ms\": 1508834952474,\n \"volume\": 0.004533691\n }\n }\n}\n

0
0 Comments

如何将JSON.stringify转换为JSON?

问题的出现原因是:JSON.stringify()函数用于将一个JavaScript对象转换为字符串,而有时我们需要将字符串转换回JavaScript对象,即将JSON.stringify()的结果转换为JSON对象。

解决方法是:使用JSON.parse()函数将JSON字符串解析为JavaScript对象。以下是一个示例:

var object = JSON.parse("string");

现在,变量object中包含了您所需的JSON对象。

0