我有以下代码获取选择条目出现的次数:
int k = 0;
int j = 0;
try {
jsonArray = new JSONArray(result);
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = (JSONObject)jsonArray.get(i); // get the josn object
if(jsonObj.getString("type").equals("image")) { // compare for the key-value
k++;
}
if(jsonObj.getString("type").equals("text")) { // compare for the key-value
j++;
}
}
Toast.makeText(getApplicationContext(), String.valueOf(k), 2000).show();
Toast.makeText(getApplicationContext(), String.valueOf(j), 2000).show();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我的 JSON 文件是:
[
{
"id": "12",
"type": "text",
"data": "This is a test"
},
{
"id": "5465465",
"type": "image",
"data": "http://pagesbyz.com/theImages/gallery/portfolio_app07.png"
},
{
"id": "982",
"type": "text",
"data": "THIS IS A TEST TEXT"
},
{
"id": "5500",
"type": "text",
"data": "http://pagesbyz.com/theImages/gallery/portfolio_app03.png"
}
]
所以...k = 1
和j = 3
如何获取匹配条目的整个数组?
例如,一旦类型等于image
我想要一个数组来保存信息。
如何在 Java 中翻译以下内容?
for (as many times an "image" entry appears) {
if ( type == "image") {
imgarr[index found][id] = "5465465";
imgarr[index found][type] = "image";
imgarr[index found][data] = "http://pagesbyz.com/theImages/gallery/portfolio_app07.png";
}
}
for (as many times an "text" entry appears) {
if ( type == "text") {
txtarr[index found][id] = "12";
txtarr[index found][type] = "text";
txtarr[index found][data] = "This is a test";
txtarr[index found][id] = "082";
txtarr[index found][id] = "text";
txtarr[index found][id] = "THIS IS A TEST TEXT";
and so forth...
}
}
同样的类型等于“文本”?