加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

JsonObject&JSONArray

发布时间:2020-12-16 19:03:05 所属栏目:百科 来源:网络整理
导读:1. Remove JSON object from JSONArray In java-json,there is no direct method to remove jsonObject,but usingjson-simple,it is simple to do so: JSONArray jsonArray = new JSONArray (); JSONObject jsonObject JSONObject jsonObject1 jsonObject2 js
1.

Remove JSON object from JSONArray

In java-json,there is no direct method to remove jsonObject,but usingjson-simple,it is simple to do so:

 JSONArray jsonArray = newJSONArray();JSONObject jsonObject JSONObject jsonObject1  jsonObject2  jsonObject.put("key1", "value1"); jsonObject1"key2""value2" jsonObject2"key3""value3" jsonArrayadd(jsonObjectjsonObject1jsonObject2 //........ Whole Json ArraySystem.outprintlnjsonArray//To remove 2nd jsonObject (index starts from 0)remove(1// Now the array will not have 2nd Object);
BUT: This method required at least API level 19.

2.

How to remove JSONArray element using Java

What you have there is an array of objects. Therefore you'll have to loop through the array and remove the necessary data fromeachobject,e.g.

for (int i  0 len  jsonArrlength< len; i++){ obj getJSONObjecti// Do your removals obj"id"// etc.}

I've assumed you're usingorg.json.JSONObjectandorg.json.JSONArrayhere,but the principal remains the same whatever JSON processing library you're using.

If you wanted to convert something like[{"id":215},{"id":216}]to[215,216]you could so something like:

 intArr  objArr intArrobjArr).getInt));}

3.

How do I remove a specific element from a JSONArray?


Try this code

ArrayList<String> list >();()ifjsonArray !=null=len++){ listgettoString());}//Remove the element from arraylistposition//Recreate JSON Array jsArray list);

Edit:UsingArrayListwill add""to the key and values. So,useJSONArrayitself

jsonstring//Excluding the item at positioni  position}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读