如何从获取请求中获取响应的长度?

时间:2019-01-23 08:47:43

标签: json reactjs

我有一系列数据,这些数据在一个数组(json文件)中包含一些对象,它将通过react显示。 这是我的代码:

ditto -c -k --keepParent -rsrc "${WORKSPACE}/build/My_Project_Test.xcarchive/dSYMs/My_Project_Test.app.dSYM" ${WORKSPACE}/build/My_Project_Test-${PRODUCT_VERSION}-${PRODUCT_VERSION}-dSYM.zip

我想找到带有提取请求的响应长度。我也想知道如何找到 renderLibrary 将显示的项目数。例如,在json.bc中,我们要在 numbersearch 范围内显示4个对象。

2 个答案:

答案 0 :(得分:0)

使用Fetch API,您可以通过在以下代码段中运行来找到json响应项的长度。我也在代码中添加了注释。

fetch('https://jsonplaceholder.typicode.com/todos')
    .then(response => {
        //below method return promise based response by converting stream object to json
        return response.json();
    }).then(json => {
        //Once succcessful callback return you can find length of number of item
        console.log(json);
        alert("Number of item:"+json.length)
    })

fetch('https://jsonplaceholder.typicode.com/todos')
    .then(response => {
        //below method return promise based response by converting stream object to json
        return response.json();
    }).then(json => {
        //Once succcessful callback return you can find length of number of item
        alert(json.length)
    })

答案 1 :(得分:0)

您可以使用状态中数据数组的长度来区分项目数。

由于数组从0开始,因此您需要将计数增加1。这是下面的示例代码片段,您可以在代码示例中使用它。

<div class="countHotel"><span class="numbersearch">{this.state.data && this.state.data.length + 1}</span></div>
相关问题