【问题标题】:Json stringify range errorJson字符串化范围错误
【发布时间】:2017-09-25 02:39:42
【问题描述】:

我从 API 得到的结果如下:

[
  {
    "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 条记录。

我想将它保存到 AsyncStorage,因此我需要对其进行字符串化。但问题是我收到JSON.stringify范围错误。 3370 结果太多要字符串化。

然后我使用lodash chunk 来拆分数组。

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

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

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

如何将其字符串化以获得:

"[
  {
    "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();

有什么想法吗?

【问题讨论】:

标签: javascript json react-native


【解决方案1】:
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 中用到的这个东西

【讨论】:

    猜你喜欢
    • 2013-09-25
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 2012-02-01
    • 2021-05-16
    相关资源
    最近更新 更多