服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android解析json数组对象的方法及Apply和数组的三个技巧

Android解析json数组对象的方法及Apply和数组的三个技巧

2021-04-24 16:36Android开发网 Android

这篇文章主要介绍了Android解析json数组对象的方法及Apply和数组的三个技巧的相关资料,需要的朋友可以参考下

json是种常用的数据传输格式,在android开发中,如何借助java语言实现对json数组对象的解析呢,请参阅下面的关键代码:

?
1
2
3
4
5
6
7
8
9
import org.json.jsonarray;
import org.json.jsonobject;
//jsondata的数据格式:[{ "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和数组:三个技巧

本文要讲的是:使用apply方法处理数组的三个技巧.

apply方法

apply是所有函数都有的方法.它的签名如下:

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

如果不考虑thisvalue的影响,上面的调用等同于:

?
1
func(arg1, arg2, ...)

也就是说,apply允许我们将一个数组"解开"成为一个个的参数再传递给调用函数.让我们分别看看apply使用中的三个技巧.

技巧1: 将一个数组传递给一个不接受数组作为参数的函数

javascript中没有返回一个数组中最大值的函数.但是,有一个函数math.max可以返回任意多个数值类型的参数中的最大值.再配合apply,我们可以实现我们的目的:

?
1
2
> math.max.apply(null, [10, -1, 5])
10

译者注:注意math.max方法的参数中只要有一个值被转为nan,则该方法直接返回nan

?
1
2
3
4
5
6
7
8
>math.max(1,null) //相当于math.max(1,0)
1
>math.max(1,undefinded) //相当于math.max(1,nan)
nan
>math.max(0,-0) //正零比负零大,和==不同
0
>math.max(-0,-1) //负零比-1大
-0

技巧2: 填补稀疏数组

数组中的缝隙

这里提醒一下读者:在javascript中,一个数组就是一个数字到值的映射.所以如果某个索引处缺失了一个元素(一条缝隙)和某个元素的值为undefined,是两种不同的情况.前者在被array.prototype中的相关方法(foreach, map, 等.)遍历时,会跳过那些缺失的元素,而后者不会:

?
1
2
3
4
5
6
7
> ["a",,"b"].foreach(function (x) { console.log(x) })
a
b
> ["a",undefined,"b"].foreach(function (x) { console.log(x) })
a
undefined
b

译者注:这里作者说"数组就是一个数字到值的映射",严格意义上是不对的,正确的说法是"数组就是一个字符串到值的映射".下面是证据:

?
1
2
>for (i in ["a", "b"]) { console.log(typeof i) //数组的索引实际上是个字符串 }"string""string">["a", "b"].foreach(function (x, i) {
console.log(typeof i) //这里的i实际上不是索引,只是个数字类型的累加器 })"number""number"

你可以使用in运算符来检测数组中是否有缝隙.

?
1
2
3
4
> 1 in ["a",,"b"]
false
> 1 in ["a", undefined, "b"]
true

译者注:这里之所以用1可以,是因为in运算符会把1转换成"1". 你过你尝试读取这个缝隙的值,会返回undefined,和实际的undefined元素是一样.

?
1
2
3
4
> ["a",,"b"][1]
undefined
> ["a", undefined, "b"][1]
undefined

译者注:[1]也会被转换成["1"]

填补缝隙

apply配合array(这里不需要加new)使用,可以将数组中的缝隙填补为undefined元素:

?
1
2
> array.apply(null, ["a",,"b"])
[ 'a', undefined, 'b' ]

这都是因为apply不会忽略数组中的缝隙,会把缝隙作为undefined参数传递给函数:

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

 

但需要注意的是,如果array方法接收到的参数是一个单独的数字,则会把这个参数当成数组长度,返回一个新数组:

?
1
2
> array.apply(null, [ 3 ])
[ , , ]

因此,最靠谱的方法是写一个这样的函数来做这种工作:

?
1
2
3
4
5
6
7
function fillholes(arr) {
var result = [];
for(var i=0; i < arr.length; i++) {
result[i] = arr[i];
}
return result;
}

执行:

?
1
2
> fillholes(["a",,"b"])
[ 'a', undefined, 'b' ]

underscore中的_.compact函数会移除数组中的所有假值,包括缝隙:

?
1
2
3
4
5
6
> _.compact(["a",,"b"])
[ 'a', 'b' ]
> _.compact(["a", undefined, "b"])
[ 'a', 'b' ]
> _.compact(["a", false, "b"])
[ 'a', 'b' ]

技巧3: 扁平化数组

任务:将一个包含多个数组元素的数组转换为一个一阶数组.我们利用apply解包数组的能力配合concat来做这件事:

?
1
2
> array.prototype.concat.apply([], [["a"], ["b"]])
[ 'a', 'b' ]

混合非数组类型的元素也可以:

?
1
2
> array.prototype.concat.apply([], [["a"], "b"])
[ 'a', 'b' ]

apply方法的thisvalue必须指定为[],因为concat是一个数组的方法,不是一个独立的函数.这种写法的限制是最多只能扁平化二阶数组:

?
1
2
> array.prototype.concat.apply([], [[["a"]], ["b"]])
[ [ 'a' ], 'b' ]

所以你应该考虑一个替代方案.比如underscore中的_.flatten函数就可以处理任意层数的嵌套数组:

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

以上所述是小编给大家分享的android解析json数组对象的方法及apply和数组的三个技巧,希望大家喜欢。

延伸 · 阅读

精彩推荐