字符串数据转换为数组问题 [英] String data conversion to array Issue

查看:95
本文介绍了字符串数据转换为数组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 https://github.com/ManifestWebDesign/angular-gridster 。这里是code为默认图表大小: -

  $ scope.dashboards = {
  1:{
    小部件:[{
      行:0,
      西:0,
      SIZEX:2,
      SIZEY:1,
      名称:Chart1
      帆布:canvas1
    },{
      行:0,
      西:2,
      SIZEX:2,
      SIZEY:1,
      名称:Chart2
      帆布:canvas2
    }]
  }
};

我要存储在数据库和放大器此默认大小;称之为负载和放大器;它适用于 $ scope.dashboards

所以我存储在数据库列中的数据是这样的: -

[{行:0,列:0,SIZEX:2,SIZEY:1,名称:Chart1,油画canvas1},{行:0,列:2,SIZEX :2,SIZEY:1,名称:Chart2,油画canvas2}]

和AJAX调用后: -

  jq.ajax({
    键入:GET,
    网址:/方法/
    成功:功能(数据){        VAR ARR = JSON.parse(数据);        $ scope.dashboards = {
            1:{
                小部件:ARR
            }
        };
    }
});

给我的错误: -

 语法错误:意外的令牌 -  [R
    在Object.parse(原生)


解决方案

JSON.parse()来是语法非常严格,因为该货币对对象的会员专区的,它应该是

字符串:值
,因此,第一,第二应该是字符串作为JSON。改变你的JSON为以下code,它应该是正确的。

 '[{行:0,山口:0,SIZEX:2,SIZEY:1,名 Chart1,帆布:canvas1},
  {行:0,COL:2,SIZEX:2,SIZEY:1,名:Chart2,帆布:canvas2}]

将工作
这个问题是之前

回答

或添加

数据类型:JSON给你的函数
 如果你将不必解析这个对象

  jq.ajax({
    键入:GET,
    网址:/方法/
    数据类型:JSON
    成功:功能(数据){        VAR ARR =数据;        $ scope.dashboards = {
            1:{
                小部件:ARR
            }
        };
    }
});

I am using https://github.com/ManifestWebDesign/angular-gridster. Here is Code for default chart sizes :-

$scope.dashboards = {
  '1': {
    widgets: [{
      row: 0,
      col: 0,
      sizeX: 2,
      sizeY: 1,
      name: "Chart1",
      canvas: "canvas1"
    }, {
      row: 0,
      col: 2,
      sizeX: 2,
      sizeY: 1,
      name: "Chart2",
      canvas: "canvas2"
    }]
  }
};

I want to store this default sizes in a database & call it on load & apply it to $scope.dashboards.

So i stored the data in database column like this :-

[{row: 0,col: 0,sizeX: 2,sizeY: 1,name: "Chart1",canvas: "canvas1"}, {row: 0,col: 2,sizeX: 2,sizeY: 1,name: "Chart2",canvas: "canvas2"}]

And after ajax call :-

jq.ajax({
    type: "GET",
    url: "/Method/",
    success: function(data) {

        var arr = JSON.parse(data);

        $scope.dashboards = {
            '1': {
                widgets: arr
            }
        };
    }
});

Gives me error :-

SyntaxError: Unexpected token r
    at Object.parse (native)

解决方案

JSON.parse() is very strict in grammer,as the pair of menber of an object,it should be

string:value , so the "first" and "second" should be string as json. change your json to following code and it should be right

'[{"row": "0","col": "0","sizeX": "2","sizeY": "1","name": "Chart1","canvas": "canvas1"},
  {"row": 0,"col": 2,"sizeX": 2,"sizeY": 1,"name": "Chart2","canvas": "canvas2"}]

it will work this question is answered before

or add

dataType:"json" to your function in case you will need not to parse this object

jq.ajax({
    type: "GET",
    url: "/Method/",
    dataType:"json",
    success: function(data) {

        var arr = data;

        $scope.dashboards = {
            '1': {
                widgets: arr
            }
        };
    }
});

这篇关于字符串数据转换为数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆