我从 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();
任何想法?
最佳答案
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 中使用的这些东西
关于javascript - Json 字符串化范围错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43655446/