2014-03-27 36 views
1

調用API服務器的結果「成功」是一個JSON文件,該文件以該字符串開頭:如何刪除「結果」:在JSON回調

{ 
    "result": "success" 
    , "data": {"total":16080,"pageCount":161,"result":[{"packWidth":250,"itemNo" 

如何去除一部分我不在乎? 就是這個

{ 
     "result": "success" 
     , "data": {"total":16080,"pageCount":161,"result": 

完整的結果是:

{ 
    "result": "success" 
    , "data": {"total":16080,"pageCount":161,"result": [{"packWidth":250,"itemNo":"1203945","groupItemNo":"1203945","status":1,"categoryId":105096,"packType":"Color Box","barcode":"6922833439687","modelLabel":"Color","packQty":24,"packInclude":"USB Cable, User Manual, USB Charger, Earphone, 1pcs Li-Battery, LCD Protector, Leather Case, Plastic Case","clearance":false,"id":103928,"packWeight":"12.500","price":"181.2800","packLength":400,"description":"description test","unitWeight":"0.726","packHeight":300}]}} 

我使用PHP語言

我不得不刪除初始部分:

{ 
    "result": "success" 
    , "data": {"total":16080,"pageCount":161,"result": 

和最後:

}} 
+0

你使用什麼語言? – Savv

+0

嗨,你可以發送完整的JSON,以便我可以幫助,也請詳細說明你的問題。謝謝 – Subh

+0

我編輯了這個問題,謝謝 – user3406254

回答

0

沒有測試,但它應該工作

preg_replace("/^.*?:(?=\w*\[) | (\w*}\w*}\w*$)/", "", $str); 

編輯:我寫這之前,我知道json_decode的,你應該使用JSON功能,如建議在fazovskys答案。

+2

但更復雜的問題,它可能是有意義的使用JSON解析器 – user1950929

+0

我報這個錯誤 PHP的警告:preg_replace函數()[function.preg-replace]:編譯失敗:丟失終止] 我可能是錯誤的腳本在php – user3406254

+0

即使有這樣簡單的問題,它是有道理的使用json解析器。 – GolezTrol

3

如果您想要使用JSON的一部分來填充CSV文件,請使用json_decode方法解析json並訪問必要的信息。

嘗試這樣:

var jsonObject = json_decode(myJson); 
var interestingPart = jsonObject.data.result; 

您現在可以訪問對象的方式中的數據。或者如果你想從它得到一個json,然後使用:

var interestingJson = json_encode(interestingPart);