2013-05-17 89 views
-2

我從MongoDB的從Json字符串脫下'[''?

一個JSON返回字符串
[{'api_calls_per_day': 0.29411764705882354, '_id': ObjectId('51948e5bc25f4b1d1c0d303a'), 'api_calls_total': 5, 'api_calls_with_key': 3, 'api_calls_without_key': 2}] 

我不想脫下小括號從上面的字符串和結果「[」和「]」需要是這樣的:

{'api_calls_per_day': 0.29411764705882354, '_id': ObjectId('51948e5bc25f4b1d1c0d303a'), 'api_calls_total': 5, 'api_calls_with_key': 3, 'api_calls_without_key': 2} 

任何想法如何從該字符串中刪除小括號?謝謝

+2

只是解析它作爲一個數組並獲得的第一個元素。 – SLaks

+2

'value [1:-1]'即使沒有解析也會執行。 – lolopop

+0

@SLaks,我是python的新手,任何示例都會非常感謝。謝謝 –

回答

2

你得到一個字符串?如果你是,它只是:

myString = MongoDBreturned[1:-1] 

這需要從{到}的子字符串。

如果它不是一個字符串,並且它實際上是一個List,那麼只需取第一個(也是唯一的元素),它是json對象。

json = MongoDBreturned[0] 
+0

謝謝,它爲我工作! –

0

這是Python,字符串是一個列表。這個怎麼樣

a = "[string with brackets]" 

a[1:-1] # string without brackets (or without first and last char) 
0

使用字符串方法:

>>> a = "[string with brackets]" 
>>> a.strip('[]') 
"string with brackets"