I am building one app in which I request a PHP file from server. This PHP file returns a JSONArray having JSONObjects as its elements e.g.,
我正在构建一个应用程序,我从服务器请求一个PHP文件。此PHP文件返回一个JSONArray,其中包含JSONObjects作为其元素,例如,
[
{
"uniqid":"h5Wtd",
"name":"Test_1",
"address":"tst",
"email":"ru_tst@tst.cc",
"mobile":"12345",
"city":"ind"
},
{...},
{...},
...
]
my code:
我的代码:
/* jArrayFavFans is the JSONArray i build from string i get from response.
its giving me correct JSONArray */
JSONArray jArrayFavFans=new JSONArray(serverRespons);
for (int j = 0; j < jArrayFavFans.length(); j++) {
try {
if (jArrayFavFans.getJSONObject(j).getString("uniqid").equals(id_fav_remov)) {
//jArrayFavFans.getJSONObject(j).remove(j); //$ I try this to remove element at the current index... But remove doesn't work here ???? $
//int index=jArrayFavFans.getInt(j);
Toast.makeText(getParent(), "Object to remove...!" + id_fav_remov, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
How do I remove a specific element from this JSONArray?
如何从此JSONArray中删除特定元素?
7 个解决方案
#1
35
Try this code
试试这个代码
ArrayList<String> list = new ArrayList<String>();
JSONArray jsonArray = (JSONArray)jsonObject;
int len = jsonArray.length();
if (jsonArray != null) {
for (int i=0;i<len;i++){
list.add(jsonArray.get(i).toString());
}
}
//Remove the element from arraylist
list.remove(position);
//Recreate JSON Array
JSONArray jsArray = new JSONArray(list);
Edit: Using ArrayList
will add "\"
to the key and values. So, use JSONArray
itself
编辑:使用ArrayList将“+”添加到键和值。所以,使用JSONArray本身
JSONArray list = new JSONArray();
JSONArray jsonArray = new JSONArray(jsonstring);
int len = jsonArray.length();
if (jsonArray != null) {
for (int i=0;i<len;i++)
{
//Excluding the item at position
if (i != position)
{
list.put(jsonArray.get(i));
}
}
}
#2
16
In case if someone returns with the same question for Android platform, you cannot use the inbuilt remove()
method if you are targeting for Android API-18 or less. The remove()
method is added on API level 19. Thus, the best possible thing to do is to extend the JSONArray
to create a compatible override for the remove()
method.
如果有人为Android平台返回相同的问题,如果您的目标是Android API-18或更低版本,则无法使用内置的remove()方法。在API级别19上添加了remove()方法。因此,最好的做法是扩展JSONArray以为remove()方法创建兼容的覆盖。
public class MJSONArray extends JSONArray {
@Override
public Object remove(int index) {
JSONArray output = new JSONArray();
int len = this.length();
for (int i = 0; i < len; i++) {
if (i != index) {
try {
output.put(this.get(i));
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
}
return output;
//return this; If you need the input array in case of a failed attempt to remove an item.
}
}
EDIT As Daniel pointed out, handling an error silently is bad style. Code improved.
编辑丹尼尔指出,静默处理错误是一种糟糕的风格。代码改进了。
#3
3
public static JSONArray RemoveJSONArray( JSONArray jarray,int pos) {
JSONArray Njarray=new JSONArray();
try{
for(int i=0;i<jarray.length();i++){
if(i!=pos)
Njarray.put(jarray.get(i));
}
}catch (Exception e){e.printStackTrace();}
return Njarray;
}
#4
2
JSONArray jArray = new JSONArray();
jArray.remove(position); // For remove JSONArrayElement
Note :- If remove()
isn't there in JSONArray
then...
注意: - 如果在JSONArray中没有remove()那么......
API 19 from Android (4.4) actually allows this method.
Android(4.4)的API 19实际上允许这种方法。
Call requires API level 19 (current min is 16): org.json.JSONArray#remove
调用需要API级别19(当前最小值为16):org.json.JSONArray #remove
Right Click on Project Go to Properties
右键单击Project转到属性
Select Android from left site option
从左侧站点选项中选择Android
And select Project Build Target greater then API 19
然后选择Project Build Target大于API 19
Hope it helps you.
希望它能帮到你。
#5
1
i guess you are using Me version, i suggest to add this block of function manually, in your code (JSONArray.java) :
我猜你正在使用我的版本,我建议在你的代码中手动添加这个功能块(JSONArray.java):
public Object remove(int index) {
Object o = this.opt(index);
this.myArrayList.removeElementAt(index);
return o;
}
In java version they use ArrayList, in ME Version they use Vector.
在java版本中,他们使用ArrayList,在ME版本中他们使用Vector。
#6
1
In my case I wanted to remove jsonobject with status as non zero value, so what I did is made a function "removeJsonObject" which takes old json and gives required json and called that function inside the constuctor.
在我的情况下,我想删除状态为非零值的jsonobject,所以我做了一个函数“removeJsonObject”,它接受旧的json并提供所需的json并在constuctor中调用该函数。
public CommonAdapter(Context context, JSONObject json, String type) {
this.context=context;
this.json= removeJsonObject(json);
this.type=type;
Log.d("CA:", "type:"+type);
}
public JSONObject removeJsonObject(JSONObject jo){
JSONArray ja= null;
JSONArray jsonArray= new JSONArray();
JSONObject jsonObject1=new JSONObject();
try {
ja = jo.getJSONArray("data");
} catch (JSONException e) {
e.printStackTrace();
}
for(int i=0; i<ja.length(); i++){
try {
if(Integer.parseInt(ja.getJSONObject(i).getString("status"))==0)
{
jsonArray.put(ja.getJSONObject(i));
Log.d("jsonarray:", jsonArray.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
jsonObject1.put("data",jsonArray);
Log.d("jsonobject1:", jsonObject1.toString());
return jsonObject1;
} catch (JSONException e) {
e.printStackTrace();
}
return json;
}
#7
0
You can use reflection
你可以使用反射
A Chinese website provides a relevant solution: http://blog.csdn.net/peihang1354092549/article/details/41957369
If you don't understand Chinese, please try to read it with the translation software.
中文网站提供相关解决方案:http://blog.csdn.net/peihang1354092549/article/details/41957369如果您不懂中文,请尝试使用翻译软件阅读。
He provides this code for the old version:
他为旧版本提供了此代码:
public void JSONArray_remove(int index, JSONArray JSONArrayObject) throws Exception{
if(index < 0)
return;
Field valuesField=JSONArray.class.getDeclaredField("values");
valuesField.setAccessible(true);
List<Object> values=(List<Object>)valuesField.get(JSONArrayObject);
if(index >= values.size())
return;
values.remove(index);
}
#1
35
Try this code
试试这个代码
ArrayList<String> list = new ArrayList<String>();
JSONArray jsonArray = (JSONArray)jsonObject;
int len = jsonArray.length();
if (jsonArray != null) {
for (int i=0;i<len;i++){
list.add(jsonArray.get(i).toString());
}
}
//Remove the element from arraylist
list.remove(position);
//Recreate JSON Array
JSONArray jsArray = new JSONArray(list);
Edit: Using ArrayList
will add "\"
to the key and values. So, use JSONArray
itself
编辑:使用ArrayList将“+”添加到键和值。所以,使用JSONArray本身
JSONArray list = new JSONArray();
JSONArray jsonArray = new JSONArray(jsonstring);
int len = jsonArray.length();
if (jsonArray != null) {
for (int i=0;i<len;i++)
{
//Excluding the item at position
if (i != position)
{
list.put(jsonArray.get(i));
}
}
}
#2
16
In case if someone returns with the same question for Android platform, you cannot use the inbuilt remove()
method if you are targeting for Android API-18 or less. The remove()
method is added on API level 19. Thus, the best possible thing to do is to extend the JSONArray
to create a compatible override for the remove()
method.
如果有人为Android平台返回相同的问题,如果您的目标是Android API-18或更低版本,则无法使用内置的remove()方法。在API级别19上添加了remove()方法。因此,最好的做法是扩展JSONArray以为remove()方法创建兼容的覆盖。
public class MJSONArray extends JSONArray {
@Override
public Object remove(int index) {
JSONArray output = new JSONArray();
int len = this.length();
for (int i = 0; i < len; i++) {
if (i != index) {
try {
output.put(this.get(i));
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
}
return output;
//return this; If you need the input array in case of a failed attempt to remove an item.
}
}
EDIT As Daniel pointed out, handling an error silently is bad style. Code improved.
编辑丹尼尔指出,静默处理错误是一种糟糕的风格。代码改进了。
#3
3
public static JSONArray RemoveJSONArray( JSONArray jarray,int pos) {
JSONArray Njarray=new JSONArray();
try{
for(int i=0;i<jarray.length();i++){
if(i!=pos)
Njarray.put(jarray.get(i));
}
}catch (Exception e){e.printStackTrace();}
return Njarray;
}
#4
2
JSONArray jArray = new JSONArray();
jArray.remove(position); // For remove JSONArrayElement
Note :- If remove()
isn't there in JSONArray
then...
注意: - 如果在JSONArray中没有remove()那么......
API 19 from Android (4.4) actually allows this method.
Android(4.4)的API 19实际上允许这种方法。
Call requires API level 19 (current min is 16): org.json.JSONArray#remove
调用需要API级别19(当前最小值为16):org.json.JSONArray #remove
Right Click on Project Go to Properties
右键单击Project转到属性
Select Android from left site option
从左侧站点选项中选择Android
And select Project Build Target greater then API 19
然后选择Project Build Target大于API 19
Hope it helps you.
希望它能帮到你。
#5
1
i guess you are using Me version, i suggest to add this block of function manually, in your code (JSONArray.java) :
我猜你正在使用我的版本,我建议在你的代码中手动添加这个功能块(JSONArray.java):
public Object remove(int index) {
Object o = this.opt(index);
this.myArrayList.removeElementAt(index);
return o;
}
In java version they use ArrayList, in ME Version they use Vector.
在java版本中,他们使用ArrayList,在ME版本中他们使用Vector。
#6
1
In my case I wanted to remove jsonobject with status as non zero value, so what I did is made a function "removeJsonObject" which takes old json and gives required json and called that function inside the constuctor.
在我的情况下,我想删除状态为非零值的jsonobject,所以我做了一个函数“removeJsonObject”,它接受旧的json并提供所需的json并在constuctor中调用该函数。
public CommonAdapter(Context context, JSONObject json, String type) {
this.context=context;
this.json= removeJsonObject(json);
this.type=type;
Log.d("CA:", "type:"+type);
}
public JSONObject removeJsonObject(JSONObject jo){
JSONArray ja= null;
JSONArray jsonArray= new JSONArray();
JSONObject jsonObject1=new JSONObject();
try {
ja = jo.getJSONArray("data");
} catch (JSONException e) {
e.printStackTrace();
}
for(int i=0; i<ja.length(); i++){
try {
if(Integer.parseInt(ja.getJSONObject(i).getString("status"))==0)
{
jsonArray.put(ja.getJSONObject(i));
Log.d("jsonarray:", jsonArray.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
jsonObject1.put("data",jsonArray);
Log.d("jsonobject1:", jsonObject1.toString());
return jsonObject1;
} catch (JSONException e) {
e.printStackTrace();
}
return json;
}
#7
0
You can use reflection
你可以使用反射
A Chinese website provides a relevant solution: http://blog.csdn.net/peihang1354092549/article/details/41957369
If you don't understand Chinese, please try to read it with the translation software.
中文网站提供相关解决方案:http://blog.csdn.net/peihang1354092549/article/details/41957369如果您不懂中文,请尝试使用翻译软件阅读。
He provides this code for the old version:
他为旧版本提供了此代码:
public void JSONArray_remove(int index, JSONArray JSONArrayObject) throws Exception{
if(index < 0)
return;
Field valuesField=JSONArray.class.getDeclaredField("values");
valuesField.setAccessible(true);
List<Object> values=(List<Object>)valuesField.get(JSONArrayObject);
if(index >= values.size())
return;
values.remove(index);
}