当数组不是对象时,在JavaScript中从JSON ajax请求获取数组长度的问题

时间:2022-10-18 07:56:29

I have a problem with getting the length of an array, when it is not initialized in the format: var variable = new Array();

我没有获取数组长度的问题,当它没有以格式初始化时:var variable = new Array();

Here is my code:

这是我的代码:

var inx;
var qns;
var qis;
var ncs;
var nzs;
var tps;

function display_question()
{

   $( "#question_no" ).text( qns[ inx ] );
   $( "#question_nc" ).text( ncs[ inx ] );
   $( "#question_nz" ).text( nzs[ inx ] );
   $( "#the_question" ).hide();
   $( "#the_question" ).text( tps[ inx ] );
   $( "#the_question" ).fadeIn( 500 );

}

function next_question()
{

   var arr_len = qns.length;

   if( inx < arr_len )
   {

      inx++;
      display_question();

   }

}

function prev_question()
{

   if( inx > 0 )
   {

      inx--;
      display_question();

   }

}

function get_questions()
{

   var url = "php/pytania.php"; 

   $.ajax(
   {

      cache: false,
      async: false,
      type: "GET",
      dataType: "text",
      url: url,
      success: function( response ) 
      { 

         data = jQuery.parseJSON( response );

         inx = 0;
         qns = data.qns;
         qis = data.qis;
         ncs = data.ncs;
         nzs = data.nzs;
         tps = data.tps;
         display_question();

      }

   } );

}

The problem is that when I try to return the length of qns like so: qns.length, it doesn't return anything. I suspect that this may be because when i read in the array from the JSON response, it is not as an array object. Is there any way to fix this? I'd appreciate any help! :)

问题是,当我尝试返回qns的长度时,如:qns.length,它不会返回任何内容。我怀疑这可能是因为当我从JSON响应中读取数组时,它不是作为数组对象。有没有什么办法解决这一问题?我很感激任何帮助! :)

1 个解决方案

#1


0  

There is no difference between arrays/objects you retrieve via JSON and those created in JavaScript. The JSON objects and arrays are parsed into JavaScript ones.
And there is actully no need to call new Array() ever.

通过JSON检索的数组/对象与使用JavaScript创建的数组/对象之间没有区别。 JSON对象和数组被解析为JavaScript。并且实际上不需要调用新的Array()。

It seems data.qnr is an object not an array. You would have to loop over the objects and count the elements yourself.

似乎data.qnr是一个对象而不是一个数组。您必须循环遍历对象并自己计算元素。

However as it seems that the object is continuous numerical keys, it would be better to create the correct output (array) from the beginning.

但是,由于对象似乎是连续的数字键,因此最好从头开始创建正确的输出(数组)。

In your comments you wrote you use json_encode( $json_array, JSON_FORCE_OBJECT ) to create the JSON output. This will convert every array, even numerical ones, into objects.

在您写的评论中,您使用json_encode($ json_array,JSON_FORCE_OBJECT)来创建JSON输出。这会将每个数组(甚至数字数组)转换为对象。

Instead, just use json_encode($json_array). By default PHP will convert associative arrays to objects in JSON and numerical arrays to ... well, arrays ;)

相反,只需使用json_encode($ json_array)。默认情况下,PHP会将关联数组转换为JSON和数值数组中的对象,以便... ...,数组;)

If you do this, data.qnr will work as expected in JavaScript.

如果这样做,data.qnr将在JavaScript中按预期工作。


Also note, instead of dataType: 'text', you can set the it to json and jQuery will parse the response automatically for you (so no need to call jQuery.parseJSON).

另请注意,代替dataType:'text',您可以将其设置为json,jQuery将自动为您解析响应(因此无需调用jQuery.parseJSON)。

#1


0  

There is no difference between arrays/objects you retrieve via JSON and those created in JavaScript. The JSON objects and arrays are parsed into JavaScript ones.
And there is actully no need to call new Array() ever.

通过JSON检索的数组/对象与使用JavaScript创建的数组/对象之间没有区别。 JSON对象和数组被解析为JavaScript。并且实际上不需要调用新的Array()。

It seems data.qnr is an object not an array. You would have to loop over the objects and count the elements yourself.

似乎data.qnr是一个对象而不是一个数组。您必须循环遍历对象并自己计算元素。

However as it seems that the object is continuous numerical keys, it would be better to create the correct output (array) from the beginning.

但是,由于对象似乎是连续的数字键,因此最好从头开始创建正确的输出(数组)。

In your comments you wrote you use json_encode( $json_array, JSON_FORCE_OBJECT ) to create the JSON output. This will convert every array, even numerical ones, into objects.

在您写的评论中,您使用json_encode($ json_array,JSON_FORCE_OBJECT)来创建JSON输出。这会将每个数组(甚至数字数组)转换为对象。

Instead, just use json_encode($json_array). By default PHP will convert associative arrays to objects in JSON and numerical arrays to ... well, arrays ;)

相反,只需使用json_encode($ json_array)。默认情况下,PHP会将关联数组转换为JSON和数值数组中的对象,以便... ...,数组;)

If you do this, data.qnr will work as expected in JavaScript.

如果这样做,data.qnr将在JavaScript中按预期工作。


Also note, instead of dataType: 'text', you can set the it to json and jQuery will parse the response automatically for you (so no need to call jQuery.parseJSON).

另请注意,代替dataType:'text',您可以将其设置为json,jQuery将自动为您解析响应(因此无需调用jQuery.parseJSON)。