米琪卡哇伊
2021-04-06 14:15:31
我已將字符串格式的數據更改為類似于[對象對象]的格式,但我想將字符串對象更改為我嘗試過json.parse的json對象,但未將其更改為json對象你能建議我哪里做錯了以及如何解決這個問題try { var timekeep = await Orders.findAndCountAll({ where: { cid: orders_info.cid, }, order: [ ['id', 'DESC'] ], limit: 1, raw: true, }); var cont1 = JSON.stringify(timekeep.rows[0]); var obj = JSON.parse(cont1);} catch (err) { console.log(err)}console.log('org data' + timekeep)console.log('data as string' + cont1);// now when I am trying to printconsole.log('data as json' + obj);console.logs的輸出org data [object Object]data as sttring{"id":4006,"mid":1,"cid":41,"wid":7138,"oid":null,"status":null,"options":null,"starttime":"2018-08-15T06:08:55.000Z","duration":null,"ordertotal":50,"counter":null,"closetime":null}data as json [object object]
3 回答

哆啦的時光機
TA貢獻1779條經驗 獲得超6個贊
據我所知,您已經將其轉換為JSON var obj = JSON.parse(cont1);
因此,您已經有了一個JSON,只是您打印它的方式是錯誤的。以逗號代替+。
console.log('data as json', obj)
+正在執行字符串連接,并且正在嘗試將字符串與對象連接

侃侃無極
TA貢獻2051條經驗 獲得超10個贊
在String concat之后,它會打印data as json [object object]。如果您放置,而不是+將正確打印該對象。在摘要中,您可以看到不同之處。
var jsonstr = '{"id":4006,"mid":1,"cid":41,"wid":7138,"oid":null,"status":null,"options":null,"starttime":"2018-08-15T06:08:55.000Z","duration":null,"ordertotal":50,"counter":null,"closetime":null}';
console.log(JSON.parse(jsonstr));
console.log('data as json' , JSON.parse(jsonstr));
console.log('data as json' + JSON.parse(jsonstr));
添加回答
舉報
0/150
提交
取消