如何使用jquery逐个解析对象到数组?

时间:2016-09-23 05:19:38

标签: javascript jquery

我有以下json数据,

{
    "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解析此值?

3 个答案:

答案 0 :(得分:0)

table

此方法将json字符串转换为对象,您可以访问unsigned int *table[100] = {NULL}; static unsigned int *table[100] = {NULL}; 中的第一项,如:

var obj = jQuery.parseJSON(json);

答案 1 :(得分:0)

请使用它。

$.param(YourjsonObj);

这会将json对象转换为字符串。

答案 2 :(得分: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);