Home>

json is a commonly used data transmission format.In android development, how to parse the json array object with the help of the java language,See the key code below:

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 array:three tips

What this article is about:Three techniques for working with arrays using the apply method.

apply method

apply is a method that all functions have.Its signature is as follows:

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

Without considering the impact of thisvalue, the above call is equivalent to:

func (arg1, arg2, ...)

That is,apply allows us to "unwrap" an array into individual arguments before passing them to the calling function.Let's take a look at each of the three techniques used in apply.

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

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 largest value among any number of numeric type parameters.Together with apply, we can achieve our purpose:

>math.max.apply (null, [10, -1, 5])
10
</pre>
</div>
<p>
Translator's Note:Note that as long as one of the parameters of the math.max method is converted to nan, the method returns nan directly
</p>
<div>
<pre>
>math.max (1, null) //equivalent to math.max (1,0)
1
>math.max (1, undefinded) //equivalent to math.max (1, nan)
nan
>math.max (0, -0) //positive zero is greater than negative zero,And == different
0
>math.max (-0, -1) //negative zero is greater than -1
-0

Tip 2:fill sparse arrays

Gaps in array

Here's a reminder to readers:In JavaScript, an array is a mapping of numbers to values.So if an element (a gap) is missing at an index and the value of an element is undefined, these are two different cases.The former will skip those missing elements when traversed by related methods (foreach, map, etc.) in array.prototype.The latter does not:

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

Translator's Note:The author here said that "array is a mapping of numbers to values", which is strictly incorrect,The correct statement is "array is a string to value mapping". Here is the evidence:

>for (i in ["a", "b"]) {console.log (typeof i) //The index of the array is actually a string} "string" "string">["a", "b "] .foreach (function (x, i) {
console.log (typeof i) //i here is not actually an index,Just a number accumulator}) "number" "number"

You can use the in operator to detect if there are gaps in the array.

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

Translator's Note:The reason why 1 is used here is because the in operator will convert 1 to "1". You try to read the value of this gap,Will return undefined, just like 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

apply is used with array (no need to add new here), you can fill the gaps in the array as undefined elements:

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

This is because apply does not ignore the gaps in the array.The gap will be passed to the function as an undefined parameter:

>function returnargs () {return [] .slice.call (arguments)}
>returnargs.apply (null, ["a" ,, "b"])
["a", undefined, "b"]

But it is important to note thatIf the parameter received by the array method is a single number,Will treat this parameter as the length of the array,Returns a new array:

>array.apply (null, [3])
[,,]

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

function fillholes (arr) {
var result=[];
for (var i=0;i<arr.length;i ++) {
result [i]=arr [i];
}
return result;
}

carried out:

>fillholes (["a" ,, "b"])
["a", undefined, "b"]

The _.compact function in underscore will remove all false values ​​from the array,Include gaps:

>_.compact (["a" ,, "b"])
["a", "b"]
>_.compact (["a", undefined, "b"])
["a", "b"]
>_.compact (["a", false, "b"])
["a", "b"]

Tip 3:flatten the array

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

>array.prototype.concat.apply ([], [["a"], ["b"]])
["a", "b"]

Mixing non-array elements is also possible:

>array.prototype.concat.apply ([], [["a"], "b"])
["a", "b"]

This value of apply method must be specified as [], because concat is an array method,Not a standalone function.The limitation of this writing is that it can only flatten the second-order array:

>array.prototype.concat.apply ([], [[["a"]], ["b"]])
[["a"], "b"]

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

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

The above are the android parsing methods and three techniques of apply and array that we share with you.Hope you like it.

  • Previous Tutorial to achieve read and write separation in MySQL
  • Next Tips for using functionapply () to parameterize arrays to improve JavaScript program performance