Android parsing json array objects and Apply and arrays of the three techniques

  • 2020-11-30 08:33:57
  • OfStack

json is a common data transfer format. In the development of android, how to parse json array objects with the help of java language? Please refer to the following key code:


import org.json.JSONArray; 
import org.json.JSONObject; 
//jsonData Data format: [{ "id": "27JpL~jd99w9nM01c000qc", "version": "abc" },{ "id": "27JpL~j6UGE0LX00s001AH", "version": "bbc" },{ "id": "27JpL~j7YkM0LX01c000gt", "version": "Wa_" }] 
JSONArray arr = new JSONArray(jsonData); 
for (int i = 0; i < arr.length(); i++) { 
JSONObject temp = (JSONObject) arr.get(i); 
String id = temp.getString("id"); 
String id = temp.getString("version"); 
}

ps: Apply and arrays :3 tips

This article covers three techniques for working with arrays using the apply method.

apply method

apply is a method that all functions have. It is signed as follows:


func.apply(thisValue, [arg1, arg2, ...]) 

Without considering the effects of thisValue, the above call is equivalent to:


func(arg1, arg2, ...) 

In other words,apply allows us to "unwrap" an array into 1 arguments that we pass to the calling function. Let's take a look at each of the three techniques apply USES.

Tip 1: Pass an array to a function that does not take an array as an argument

There is no function in JavaScript that returns the maximum value of 1 array. However, there is a function Math.max that can return the maximum value of any number of numeric types of arguments. Together with apply, we can achieve our goals:


> Math.max.apply(null, [10, -1, 5])
10

Note that the Math. max method returns NaN only if one of its arguments is converted to NaN


>Math.max(1,null) // The equivalent of Math.max(1,0)
1
>Math.max(1,undefinded) // The equivalent of Math.max(1,NaN)
NaN
>Math.max(0,-0) // Plus zero is greater than minus zero , and == different 
0
>Math.max(-0,-1) // Negative zero than -1 big 
-0 

Tip 2: Fill a sparse array

Gaps in the array

A reminder to the reader: in JavaScript, an array is a mapping from a number to a value. So if an element is missing from an index (a gap) and an element has a value of undefined, there are two different situations. The former is traversed by the related methods in ES55en.prototype (forEach, map, etc.), which will skip those missing elements, while the latter will not:


> ["a",,"b"].forEach(function (x) { console.log(x) })
a
b
> ["a",undefined,"b"].forEach(function (x) { console.log(x) })
a
undefined
b

Here the author says that "array is a mapping from a number to a value ", which is technically incorrect, but the correct statement is that" array is a mapping from a string to a value ". Here's the evidence:


>for (i in ["a", "b"]) { console.log(typeof i) // The index of an array is actually a string  }"string""string">["a", "b"].forEach(function (x, i) { 
console.log(typeof i) // Here, i It's not actually an index , It's just an accumulator of type number  })"number""number" 

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


> 1 in ["a",,"b"]
false
> 1 in ["a", undefined, "b"]
true 

Translator's note: 1 works because the in operator converts 1 to "1". If you try to read the value of this gap, it will return undefined, the same as the actual undefined element.


> ["a",,"b"][1]
undefined
> ["a", undefined, "b"][1]
undefined 

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

Fill the gap

Using apply in conjunction with Array(new is not needed here), the gaps in the array can be filled into undefined elements:


> Array.apply(null, ["a",,"b"])
[ 'a', undefined, 'b' ]

This is because apply does not ignore gaps in the array, passing gaps as undefined parameters to the function:


func.apply(thisValue, [arg1, arg2, ...]) 
0

Note, however, that if the Array method receives an argument of a single number, it will treat this argument as the length of the array and return a new array:


func.apply(thisValue, [arg1, arg2, ...]) 
1

Therefore, the most reliable method is to write a function like this to do the work:


func.apply(thisValue, [arg1, arg2, ...]) 
2

Perform:


> fillHoles(["a",,"b"])
[ 'a', undefined, 'b' ] 

The _.compact function in Underscore removes all false values in the array, including gaps:


func.apply(thisValue, [arg1, arg2, ...]) 
4

Tip 3: Flatten arrays

Task: Convert an array containing multiple array elements into a 1-order array. We use apply's ability to unpack arrays in conjunction with concat to do this:


func.apply(thisValue, [arg1, arg2, ...]) 
5

Mixed elements of non-array type can also be:


func.apply(thisValue, [arg1, arg2, ...]) 
6

The thisValue of the apply method must be specified as [], because concat is a method of an array, not a stand-alone function. The limit of this writing is to flatten 2-order arrays at most:


func.apply(thisValue, [arg1, arg2, ...]) 
7

So you should consider an alternative. For example, the _.flatten function in Underscore can handle nested arrays of any number of layers:


> _.flatten([[["a"]], ["b"]])
[ 'a', 'b' ]

Above is the site to share Android parsing json array objects and Apply and arrays of the 3 tips, I hope you like.


Related articles: