JSONArray列出从结果另一个阵列? [英] JSONArray list another array from result?
本文介绍了JSONArray列出从结果另一个阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要能够从一个我已经创建一个新的JSONArray,阵列中的我有一大堆的字段名为id,另每行一个,我该如何让他们的名单?
如。我得到这个回
[{ID:111,用户:斑点},
{ID:111,用户:斑点},
{ID:111,用户:斑点},
{ID:111,用户:斑点},
{ID:111,用户:斑点},
{ID:111,用户:斑点}]
我将如何得到公正编号的列表?
- 编辑
决定在此之后但由于直接使用一个for循环:D
的Htt presponse响应= httpclient.execute(HTTPGET); //执行HttpPost要求
JSONArray JSA = projectResponse(响应); //获取JSON响应的for(int i = 0; I< jsa.length();我++){
JSONObject的JSO = jsa.getJSONObject(I)
publishProgress(jso.getString(ID),jso.getString(名字));
}
解决方案
尝试了这一点:
JSONArray yourJSONArray;
清单<串GT; tempIDStringCollection =新的List<串GT;();
...的for(int i = 0; I< yourJSONArray.length();我++){
串ID = yourJSONArray.getJSONObject(ⅰ).getString(ID);
tempIDStringCollection.add(ID);
}
然后转移到另一个JSONArray这个问题,尝试:
JSONArray newArray =新JSONArray(tempIDStringCollection);
要跳过列表
创作,尝试
JSONArray yourJSONArray;
JSONArray newArray =新JSONArray();
...的for(int i = 0; I< yourJSONArray.length();我++){
串ID = yourJSONArray.getJSONObject(ⅰ).getString(ID);
newArray.put(I,ID);
}
I need to be able to create a new JSONArray from the one I already have, in the array I have a whole lot of fields called id, another one on each line, how do I get a list of them?
eg. I get this back
[{"id":111,"users":"blob"},
{"id":111,"users":"blob"},
{"id":111,"users":"blob"},
{"id":111,"users":"blob"},
{"id":111,"users":"blob"},
{"id":111,"users":"blob"}]
How would I get a list of just the ID's ?
- Edit
Decided to use a for loop straight after this but thanks :D
HttpResponse response = httpclient.execute(httpget); //execute the HttpPost request
JSONArray jsa = projectResponse(response); //get the json response
for(int i = 0; i < jsa.length(); i++){
JSONObject jso = jsa.getJSONObject(i);
publishProgress(jso.getString("id"), jso.getString("name"));
}
解决方案
Try this out:
JSONArray yourJSONArray;
List<String> tempIDStringCollection = new List<String>();
...
for(int i = 0; i < yourJSONArray.length(); i++){
String id = yourJSONArray.getJSONObject(i).getString("id");
tempIDStringCollection.add(id);
}
Then to transfer this into another JSONArray, try:
JSONArray newArray = new JSONArray(tempIDStringCollection);
To skip the List
creation, try
JSONArray yourJSONArray;
JSONArray newArray = new JSONArray();
...
for(int i = 0; i < yourJSONArray.length(); i++){
String id = yourJSONArray.getJSONObject(i).getString("id");
newArray.put(i, id);
}
这篇关于JSONArray列出从结果另一个阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文