2016-09-23 61 views
0

我有以下JSON數據,如何使用jquery逐個解析對象到數組?

{ 
    "componants": [{ 
     "category": "screensets", 
     "position": "top", 
     "rotate": "180", 
     "3d_file": "3d_deg_180.obj", 
     "height": "10", 
     "width": "10", 
     "x": "239", 
     "y": "186", 
     "current_roate": "0", 
     "comp_color": "" 
    }, { 
     "category": "screensets", 
     "position": "top", 
     "rotate": "180", 
     "3d_file": "3d_deg_180.obj", 
     "height": "10", 
     "width": "10", 
     "x": "619", 
     "y": "186", 
     "current_roate": "0", 
     "comp_color": "" 
    }, { 
     "category": "screensets", 
     "position": "top", 
     "rotate": "180", 
     "3d_file": "3d_deg_180.obj", 
     "height": "10", 
     "width": "10", 
     "x": "379", 
     "y": "86", 
     "current_roate": "0", 
     "comp_color": "" 
    }], 
    "width": "640", 
    "height": "640", 
    "name": "Test Drawing", 
    "size": "40", 
    "screen": "Conference set" 
} 

如何分析使用jquery這個價值?

+2

要分析是什麼?你想轉換成字符串 ?你能詳細解釋一下嗎 –

+1

'obj ['componants']'是一個數組 – passion

+0

請[編輯]你的問題來顯示所需的輸出格式是什麼。你是什​​麼意思「一個接一個」? – nnnnnn

回答

0

你是否嘗試操縱你的對象像下面這樣的數組?

[ 
    { 
    "width" : "640", 
    "height" : "640", 
    "name"  : "Test Drawing", 
    "size"  : "40", 
    "screen" : "Conference set", 
    "category" : "screensets", 
    "position" : "top", 
    "rotate" : "180", 
    "3d_file" : "3d_deg_180.obj", 
    "height" : "10", 
    "width" : "10", 
    "x" : "379", 
    "y" : "86", 
    "current_roate" : "0", 
    "comp_color" : "" 
    } 

    ... 
] 

你可以試試這個:)

var obj = { ... }; // your object 
obj = $.map(obj.componants, 
    function(val, idx) { 
    return $.extend(
     { width : obj.width, 
     height: obj.height, 
     name : obj.name, 
     size : obj.size, 
     screen: obj.screen 
     }, val); 
0
var obj = jQuery.parseJSON(json); 

這種方法轉換JSON字符串對象,你可以在components訪問的第一個項目就是喜歡:

obj.components[0].category 
0

請使用它。

$.param(YourjsonObj); 

這會將json對象轉換爲字符串。

+0

OP要求「解析」 - 轉換爲字符串與解析相反。 – nnnnnn