带有数组逗号的JSON解析器分隔Javascript

时间:2015-07-16 07:49:22

标签: javascript jquery arrays json html5

我有一个JSON结构,看起来像这样。

"content" : {
    "state" : {
      "Idle" : [
        [
          78.366666667,
          1436955825
        ],
        [
          71.281111111,
          1436955840
        ],
        [
          70.41,
          1436955855
        ],
        [
          74.283333333,
          1436955870
        ],
        [
          76.411111111,
          1436955885
        ]
       ] 
        } }

我要做的就是解析这个值。我必须将逗号分隔值分隔为x和y。我必须解析这些值并将它们分开。我有解决问题的问题

它看起来应该是这样的

 "Idle" : [
            {
              "x" : 78.366666667,
              "y" :1436955825
            },
            {
              "x" :71.281111111,
              "y" :1436955840
            },
            {
              "x" :70.41,
              "y" :1436955855
            },
            {
              "x" :74.283333333,
              "y" :1436955870
            },
            {
              "x" :76.411111111,
              "y" :1436955885
            }
           ]

5 个答案:

答案 0 :(得分:3)

...Idle元素映射到对象(请Array.map查看test = { content: { state: { Idle: [ [ 78.366666667, 1436955825 ], [ 71.281111111, 1436955840 ], [ 70.41, 1436955855 ], [ 74.283333333, 1436955870 ], [ 76.411111111, 1436955885 ] ] } } }; test.content.state.Idle = test.content.state.Idle.map( function (v) { return { x: v[0], y: v[1] }; } ); document.querySelector("#result").textContent = JSON.stringify(test, null, " ");):



<pre id="result"></pre>
&#13;
add_filter( 'the_title', 'ta_modified_post_title');
function ta_modified_post_title ($title) {
  if ( in_the_loop() && !is_page() ) {
    $title = "Test (modified title)"; // your title name
  }
  return $title;
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

var container = {
    "content": {
        "state": {
            "Idle": [
                [
                    78.366666667,
                    1436955825
                ],
                [
                    71.281111111,
                    1436955840
                ],
                [
                    70.41,
                    1436955855
                ],
                [
                    74.283333333,
                    1436955870
                ],
                [
                    76.411111111,
                    1436955885
                ]
            ]
        }
    }
};

var _oldIdle = container.content.state.Idle,
    _newIdle = [];

for (var i = 0; i < _oldIdle.length; i++) {
    _newIdle.push({
        x: _oldIdle[i][0],
        y: _oldIdle[i][1]
    });
}

container.content.state.Idle = _newIdle;

答案 2 :(得分:0)

尝试这种方式:

var json = {
  "content": {
    "state": {
      "Idle": [
        [
          78.366666667,
          1436955825
        ],
        [
          71.281111111,
          1436955840
        ],
        [
          70.41,
          1436955855
        ],
        [
          74.283333333,
          1436955870
        ],
        [
          76.411111111,
          1436955885
        ]
      ]
    }
  }
};

var newObj = {},
  arr = [];

$.each(json.content.state.Idle, function(i, item) {
  arr.push({x: item[0], y: item[1]});
});
newObj.idle = arr;
console.log(newObj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 3 :(得分:0)

如果您有JSON字符串,则可以将JSON.parsereviver function

一起使用

&#13;
&#13;
var json = '{"content":{"state":{"Idle":[[78.366666667,1436955825],[71.281111111,1436955840],[70.41,1436955855],[74.283333333,1436955870],[76.411111111,1436955885]]}}}';
var result = JSON.parse(json, function(k, v) {
  if ((v instanceof Array) && (isFinite(Number(k)))) {
    //if array with coordinates - return object instead
    return {
      x: v[0],
      y: v[1]
    };
  }
  return v;
})
console.log(result);
document.getElementById('r').innerHTML = JSON.stringify(result,null,2)
&#13;
<pre id='r'></pre>
&#13;
&#13;
&#13;

答案 4 :(得分:-1)

使用UnderscoreJS很简单:

JSFiddle

var values = {
    "content": {
        "state": {
            "Idle": [
                [
                78.366666667,
                1436955825],
                [
                71.281111111,
                1436955840],
                [
                70.41,
                1436955855],
                [
                74.283333333,
                1436955870],
                [
                76.411111111,
                1436955885]
            ]
        }
    }
};

var newValues = _.map(values.content.state.Idle, function (value, key) {
    return {x: value[0], y: value[1]};
});

console.log(newValues);