Android’s method of parsing JSON array objects and three skills of apply and array

JSON is a common data transmission format. In Android development, how to use java language to parse JSON array objects? Please refer to the following key codes:

PS: apply and array: Three Tips

This article will talk about three techniques of using the apply method to deal with arrays

Apply method

Apply is a method common to all functions. Its signature is as follows:

If the influence of thisvalue is not considered, the above call is equivalent to:

In other words, apply allows us to "untangle" an array into parameters one by one and then pass them to the calling function. Let's take a look at the three techniques used by apply

Tip 1: pass an array to a function that does not accept an array as an argument

There is no function in JavaScript that returns the maximum value in an array. However, there is a function math.max that can return the maximum value in any number of numeric parameters. Combined with apply, we can achieve our purpose:

Note: note that as long as one of the parameters of the math.max method is converted to Nan, the method directly returns Nan

Tip 2: fill sparse arrays

Gap in array

Here's a reminder to readers: in JavaScript, an array is a number to value mapping. Therefore, if an element (a gap) is missing at an index and the value of an element is undefined, there are two different situations. The former will skip those missing elements when traversed by the relevant methods (foreach, map, etc.) in array.prototype, and the latter will not:

Note to the translator: the author here says that "an array is a number to value mapping", which is strictly wrong. The correct statement is "an array is a string to value mapping". The following is the evidence:

You can use the in operator to detect gaps in the array

Note: the reason why 1 is used here is that the in operator will convert 1 to "1". If you try to read the value of this gap, you will return undefined, which is the same as the actual undefined element

Translator's note: [1] will also be converted to ["1"]

Fill the gap

Apply is used in conjunction with array (no need to add new here) to fill the gap in the array as undefined elements:

This is because apply will not ignore the gap in the array and will pass the gap to the function as an undefined parameter:

However, it should be noted that if the parameter received by the array method is a single number, the parameter will be regarded as the length of the array and a new array will be returned:

Therefore, the most reliable way is to write such a function to do this work:

Execution:

In underscore The compact function removes all false values from the array, including gaps:

Tip 3: flatten arrays

Task: convert an array containing multiple array elements into a first-order array. We use the ability of apply to unpack the array in conjunction with concat to do this:

Elements of mixed non array types can also:

The thisvalue of the apply method must be specified as [], because concat is an array method, not an independent function. The limitation of this writing method is that it can only flatten second-order arrays at most:

So you should consider an alternative. For example The flatten function can handle nested arrays of any number of layers:

The above is the Android method of parsing JSON array objects and the three skills of apply and array shared by Xiaobian. I hope you like it.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>