Json stringify范围错误

时间:2017-04-27 10:41:21

标签: javascript json react-native

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

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

def _process_content(contents):
    for raw_line in contents:
        if "Test case" in raw_line:
            processed.append(raw_line)
        elif "Current Parameters" in raw_line:
            processed.append(raw_line)
        elif "Param" in raw_line:
            processed.append(raw_line)
        elif "PASS" in raw_line or "FAIL" in raw_lines:
            processed.append(raw_line)
            processed.append("\n")
    return processed

server.js中使用的东西