Json字符串化范围错误 [英] Json stringify range error

查看:119
本文介绍了Json字符串化范围错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从API得到如下结果:

I'm getting result from API as follows:

[
  {
    "id": 1,
    "area": "",
    "zone": "T",
    "aisle": "",
    "side": "E",
    "col": 1,
    "level": 0,
    "position": 0,
    "name": "T - E - 1"
  },
  {
    "id": 2,
    "area": "",
    "zone": "T",
    "aisle": "",
    "side": "E",
    "col": 60,
    "level": 0,
    "position": 0,
    "name": "T - E - 60"
  },
  ....
  ,
  {
    "id": 3370,
    "area": "",
    "zone": "T",
    "aisle": "",
    "side": "E",
    "col": 60,
    "level": 0,
    "position": 0,
    "name": "T - E - 60"
  }
]

结果有3370条记录.

The result has 3370 records.

我想将其保存到AsyncStorage,因此需要对其进行字符串化.但是问题是我收到JSON.stringify范围错误. 3370的结果有待确定.

I want to save it to AsyncStorage, thus I need to stringify it. But the problem is that I get an range error for JSON.stringify. 3370 result is to much to stringify.

然后,我使用了 lodash块来拆分数组.

Then I used lodash chunk to split the array.

let responseDataChunked = chunk(responseData.slots, 100);

我得到了34个数组的结果.

And I got the result of 34 arrays.

let result = [
    [{....}, {....}, ...{....}], // 0: 100 objects
    [{....}, {....}, ...{....}], // 1: 100 objects
    ..... 
    [{....}, {....}, ...{....}], // 34: 70 objects
]

如何对其进行字符串化处理:

How can I stringify it to get :

"[
  {
    "id": 1,
    "area": "",
    "zone": "T",
    "aisle": "",
    "side": "E",
    "col": 1,
    "level": 0,
    "position": 0,
    "name": "T - E - 1"
  },
  {
    "id": 2,
    "area": "",
    "zone": "T",
    "aisle": "",
    "side": "E",
    "col": 60,
    "level": 0,
    "position": 0,
    "name": "T - E - 60"
  },
  ....
  {
    "id": 3370,
    "area": "",
    "zone": "T",
    "aisle": "",
    "side": "E",
    "col": 60,
    "level": 0,
    "position": 0,
    "name": "T - E - 60"
  }
]"

我尝试的是:

fetch(data_url + '/manager/transport/sync/slots/')
            .then(response => response.json())
            .then(responseData => {
                let max_count = responseData.slots.length;
                let current_count = max_count;

                let responseDataChunked = chunk(responseData.slots, 100);
                let jsonData = [];

                for (let i = 0; i < responseDataChunked.length; i++) {
                    let data = [];

                    for (let j = 0; j < responseDataChunked[i].length; j++){
                        let result = responseDataChunked[i][j];
                        let slot = {
                            id: j + 1,
                            area: result.area || '',
                            zone: result.zone || '',
                            aisle: result.aisle || '',
                            side: result.side || '',
                            col: result.col || 0,
                            level: result.level || 0,
                            position: result.position || 0,
                            name: Location.slotName(result)
                        };
                        data.push(slot);
                    }

                    jsonData.push(JSON.stringify(data));
                }

                //jsonData here is:
                [
                    "[{....}, {....}, ...{....}]", // 0: 100 objects
                    "[{....}, {....}, ...{....}]", // 1: 100 objects
                     ..... 
                    "[{....}, {....}, ...{....}]" // 34: 70 objects
                ]


                for (let k = 0; k < responseData.slots.length; k++) {

                    for (let l = 0; l < jsonData.length; l++){
                        AsyncStorage.setItem('slots', jsonData[l], () => {
                            current_count--;
                            counter_cb(max_count - current_count, max_count);
                            if (current_count <= 0) cb();
                        })
                    }


                }
                if (max_count === 0) cb();
            }).done();

有什么主意吗?

推荐答案

var jsonParser = bodyParser.json({ limit: 1024 * 1024 * 20, type: 'application/json' });
var urlencodedParser = bodyParser.urlencoded({ extended: true, limit: 1024 * 1024 * 20, type: 'application/x-www-form-urlencoding' })
app.use(jsonParser);
app.use(urlencodedParser);

server.js中使用的东西

this things used in server.js

这篇关于Json字符串化范围错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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