json.stringify片阵列字 [英] json.stringify slice array to word

查看:125
本文介绍了json.stringify片阵列字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 JSON.stringify(数据); 来返回JSON

这是我的code

var data;
programService.query({
    id: $routeParams.id
    }, function (result) {
    data = { 'program': result };
    data= JSON.stringify(data);
    $scope.setPagingData(data,page,pageSize);
    });

$scope.setPagingData = function(data, page, pageSize){
    var pagedData = data.slice((page - 1) * pageSize, page * pageSize);
    $scope.myData = pagedData;
    $scope.totalServerItems = data.length;
    if (!$scope.$$phase) {
        $scope.$apply();
    }
};

JSON数据

{programId:1, 
 programName:project1, 
 programContent:content1, 
 programStartDate:2012-01-01, 
 templateId: "1"}

和结果将是P,R,邻...

and the result will be p , r, o...

不是programId,1,PROGRAMNAME,PROJECT1 .....

not a programId, 1, programName, project1.....

什么想法?

推荐答案

我不知道正是你正在尝试做的,但我只想指出,有些事情在你的code,它可能指向您在正确的方向。

I'm not sure exactly what you are trying to do, but let me just point out some things in your code that could potentially point you in the right direction.

首页后,您的JSON数据(我假设你的意思是,作为结果从你的服务器:

First, your JSON data (which I assume you mean as the result from your server:

{programId:1, 
programName:project1, 
programContent:content1, 
programStartDate:2012-01-01, 
templateId: "1"}

为pretty体面恶意形成的。键和值围绕双引号是正确的JSON格式(即是数字不需要引号值)。

is pretty decently mal-formed. Double-quotes around both keys and values is proper JSON format (values that are numbers don't need quotes).

,假设你的JSON数据确实是你怎么会想到它是在你的回调函数:

Second, assuming your JSON data is indeed how you want and expect it to be, in your callback function:

...
    }, function (result) {
      data = {
        'program': result
      };
      ...
    });
...

结果 STRING (因为JSON是一个简单的基于文本的序列化格式,并始终是一个字符串),意为

result is a STRING (since JSON is simply a text-based serialization format and is always a string), meaning

data = {
  'program': result
};

结果数据看起来像一个JS对象是这样的:

results in data looking like a JS object like this:

{ 'program' : '{programId:1, programName:project1, programContent:content1, programStartDate:2012-01-01, templateId: "1"}' }

或许,你的意思是到JSON转换为本地JS对象,在这种情况下, JSON.parse(结果)是你在找什么。

data = {
  'program': JSON.parse(result)
};

数据现在看起来是这样的JS对象(假设是国内形成的JSON,否则解析功能,将打破

data now looks like a JS object like this (assuming your JSON is well-formed, otherwise the parse function will break):

{
    "program" : {
        "programId" : 1,
        "programName" : "project1",
        "programContent" : "content1",
        "programStartDate" : "2012-01-01",
        "templateId" : "1"
    }
}


第三后,回到你的回调函数:


Third, back to your callback function:

...
    }, function (result) {
      ...
      data= JSON.stringify(data);
      $scope.setPagingData(data,page,pageSize);

    });
...

数据= JSON.stringify(数据); 给定的数据转换成 JSON字符串。如果这是你想要什么,然后再进行!

data= JSON.stringify(data); converts the given data into a JSON string. If this is what you intend, then carry on!

第四,在 $ scope.setPagingData 功能,

$scope.setPagingData = function(data, page, pageSize){
    var pagedData = data.slice((page - 1) * pageSize, page * pageSize);
    $scope.myData = pagedData;
    $scope.totalServerItems = data.length;
    if (!$scope.$$phase) {
        $scope.$apply();
    }
};

.slice 功能和。长度属性是数组原型的两个组成部分。这意味着如果你在数据传递是一个JS对象, .slice 将抛出一个没有办法片的错误和。长度将返回undefined。

the .slice function and .length attribute are both part of the Array prototype. This means if your passed in data is a JS object, .slice will throw a "has no method slice" error and .length will return undefined.

这篇关于json.stringify片阵列字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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