Android methods for parsing JSON array objects and three techniques for apply and arrays _android

Source: Internet
Author: User
Tags array length compact

JSON is a commonly used data transmission format, in the Android development, how to use the Java language to implement the JSON array object parsing, see 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: three tips

Here's what this article is about: three tips for working with arrays using the Apply method.

Apply method

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

 
 

If the Thisvalue effect is not considered, the above invocation is equivalent to:

 
 

That is, apply allows us to "untie" an array into parameters and then pass it to the calling function. Let's look at the three tips 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 largest value in an array. However, there is a function Math.max that can return the maximum value in the parameters of any number of numeric types. In conjunction with apply, we can achieve our goal:

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

Note: Notice that whenever a value in the parameter of the Math.max method is converted to Nan, the method returns directly to Nan

>math.max (1,null)//equivalent to Math.max (1,0)
1
>math.max (1,undefinded)//equivalent Math.max (1,nan)
NaN
>math.max (0,-0)//positive zero ratio minus 0, and = = different
0
>math.max ( -0,-1)//minus 0-1 large

Tip 2: Fill a sparse array

Gaps in the array

Here's a reminder to readers: in JavaScript, an array is a map of numbers to values. So if an index is missing an element (a gap) and the value of an element is undefined, are two different situations. The former is related to the method of being Array.prototype (ForEach, map, etc.) When traversing, the missing elements are skipped, and 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

The author says, "An array is a map of numbers to values," which is strictly incorrect, and the correct argument is that "an array is a map of a string to a value." Here's 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) { 

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

> 1 in ["A",, "B"]
false
> 1 in ["A", Undefined, "B"]

Translator Note: 1 can be used here because the in operator converts 1 to "1". You have tried to read the value of this gap, will return undefined, and the actual undefined element is the same.

> ["A",, "B"][1]
undefined
> ["A", Undefined, "B"][1]

Translator Note: [1] will also be converted to ["1"]

Fill the gap

Apply with array (no need to add new here) to 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, passing the gap as a undefined parameter to the function:

> Function Returnargs () {return [].slice.call (arguments)}
> Returnargs.apply (NULL, ["A",, "B"])

Note, however, that if the array method receives an argument that is a separate number, the parameter is treated as an array length, and a new array is returned:

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

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

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

Perform:

> Fillholes (["A", "B"])

The _.compact function in underscore shifts all the false values in the array, including the gaps:

> _.compact (["A",, "B"])
[' A ', ' B ']
> _.compact (["A", Undefined, "B"])
[' A ', ' B ']
> _.com Pact (["A", false, "B"])

Tip 3: flattened arrays

Task: Converts an array that contains multiple array elements to a first-order array. We use the ability to unpack the array to do this with concat:

> Array.prototype.concat.apply ([], [["A"], ["B"]])

elements that mix non-array types can also:

> Array.prototype.concat.apply ([], [["A"], "B"])
[' A ', ' B ']

The thisvalue of the Apply method must be specified as [] because the concat is a method of an array, not a separate function. The limit to this notation is to flatten only the second-order arrays:

> Array.prototype.concat.apply ([], [[[[] a]], ["B"]])
[[' A '], ' B ']

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

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

The above is a small set to share the Android method of parsing JSON array objects and apply and several of the three techniques, I hope you like.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.