Concate JSONArray [英] Concate JSONArray
问题描述
我使用 JSONArray 在 org.json
包。
我的第一个JSONArray是这样的:
My first JSONArray is like:
[[249404,VPR249404],[249403,VPR249403],[249391,M249391]]
[["249404","VPR249404"],["249403","VPR249403"],["249391","M249391"]]
和二
[[249386,M249386],[249385,M249385(Ⅰ)],[249384,I249384]]
[["249386","M249386"],["249385","M249385(I)"],["249384","I249384"]]
所以我想追加新的JSONArray给我的第一JSONArray。
So I'd like to append new JSONArray to my first JSONArray.
我工作的Java和Android。我听说过谷歌GSON 库,但我不知道它是否能帮助我或没有,但我不希望任何其他的依赖在我的Android应用程序。
I am working on Java and Android. I have heard about google-gson library, but I don't know whether it can help me or not but I don't want any other dependency in my Android Application.
推荐答案
我会尝试这样的:
private JSONArray concatArray(JSONArray arr1, JSONArray arr2)
throws JSONException {
JSONArray result = new JSONArray();
for (int i = 0; i < arr1.length(); i++) {
result.put(arr1.get(i));
}
for (int i = 0; i < arr2.length(); i++) {
result.put(arr2.get(i));
}
return result;
}
我没有一个编译器,现在来测试,但你可以给它一个尝试,看看它是否工作(或者,至少,它给你如何做到这一点的想法)。
I don't have a compiler right now to test, but you can give it a try and see if it works (or, at least, it gives you an idea of how to do it).
修改
该版本可以Concat的多阵列( concatArray(ARR1,ARR2,ARR3)
):
This version could concat multiple arrays (concatArray(arr1, arr2, arr3)
):
private JSONArray concatArray(JSONArray... arrs)
throws JSONException {
JSONArray result = new JSONArray();
for (JSONArray arr : arrs) {
for (int i = 0; i < arr.length(); i++) {
result.put(arr.get(i));
}
}
return result;
}
这篇关于Concate JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!