2012-11-22 57 views
0

我從UI獲取數據如下,所有下面的輸入數據都是字符串。如何將字符串值作爲json對象存儲在mongo數據庫中?

CUST_ID:temp001 cust_chart_id:測試 default_chart:假 chart_pref_json:{範圍:6M,CHART_TYPE:燭臺,指標:{},週期:每日,futurepadding:20}}

我試圖存儲mongodb中的chart_pref_json。這chart_pref_json對象實際上是存儲在數據庫如下字符串,

{ "_id" : ObjectId("50aca4caf5d0b0e4d31ef239"), "cust_id" : "temp001", "cust_chart_id" : "testing", "default_chart" : "false", "created_at" : NumberLong("1353491658551"), **"chart_pref_json" : "{range:6m,chart_type:candlestick,indicators:{},period:Daily,futurepadding:20}" }** 

但其實我是想這chart_pref_json存儲爲如下的JSON對象。

{ "_id" : ObjectId("50aca4caf5d0b0e4d31ef239"), "cust_id" : "temp001", "cust_chart_id" : "testing", "default_chart" : "false", "created_at" : NumberLong("1353491658551"), **"chart_pref_json" : {range:6m,chart_type:candlestick,indicators:{},period:Daily,futurepadding:20} }** 

任何人都可以幫助我解決這個問題。

+0

當我試圖保存「chart_pref_json」:{range:6m,chart_type:燭臺,指標:{},句號:Daily,futurepadding:20}}在mongodb中,它保存爲字符串「{range:6m, chart_type:candlestick,指標:{},期間:Daily,futurepadding:20}「而不是我想要存儲爲json {範圍:6m,chart_type:燭臺,指標:{},期:Daily,futurepadding:20} 。 – Sandy

回答

0

在將其設置爲應用程序中的字段之前,需要使用您的語言的JSON解碼功能將其作爲對象進行編碼。在PHP中,你會做的是這樣的:

$db->collection->insert(array(
    'cust_id' => 'temp001', 
    … your other fields … 
    'chart_pref' => json_decode($varContainingJson) 
)); 

對於Java,下面的例子將幫助:

BasicDBObject obj = JSON.parse(varContainingJson); 

其中在https://groups.google.com/forum/?fromgroups=#!topic/mongodb-user/Y3KyG_ZSfJg

相關問題