Json stringify range error

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

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"   } ] 

The result has 3370 records.

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.

Then I used lodash chunk to split the array.

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

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"   } ]" 

What I tried is :

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(); 

Any idea?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!